标签:八进制 第一个 ack sig 包含 精度 border func 1.5
Python的字符串格式化有两种方式: 百分号方式、format方式
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。[PEP-3101]
This PEP proposes a new system for built-in string formatting operations, intended as a replacement for the existing ‘%‘ string formatting operator.
1、百分号方式
%[(name)][flags][width].[precision]typecode
注:Python中百分号格式化是不存在自动将整数转换成二进制表示的方式
常用格式化:
1
2
3
4
5
6
7
8
9
10
11
|
tpl = "i am %s" % "alex" tpl = "i am %s age %d" % ( "alex" , 18 ) tpl = "i am %(name)s age %(age)d" % { "name" : "alex" , "age" : 18 } tpl = "percent %.2f" % 99.97623 tpl = "i am %(pp).2f" % { "pp" : 123.425556 , } tpl = "i am %.2f %%" % { "pp" : 123.425556 , } |
2、Format方式
[[fill]align][sign][#][0][width][,][.precision][type]
常用格式化:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
tpl = "i am {}, age {}, {}" . format ( "seven" , 18 , ‘alex‘ ) tpl = "i am {}, age {}, {}" . format ( * [ "seven" , 18 , ‘alex‘ ]) tpl = "i am {0}, age {1}, really {0}" . format ( "seven" , 18 ) tpl = "i am {0}, age {1}, really {0}" . format ( * [ "seven" , 18 ]) tpl = "i am {name}, age {age}, really {name}" . format (name = "seven" , age = 18 ) tpl = "i am {name}, age {age}, really {name}" . format ( * * { "name" : "seven" , "age" : 18 }) tpl = "i am {0[0]}, age {0[1]}, really {0[2]}" . format ([ 1 , 2 , 3 ], [ 11 , 22 , 33 ]) tpl = "i am {:s}, age {:d}, money {:f}" . format ( "seven" , 18 , 88888.1 ) tpl = "i am {:s}, age {:d}" . format ( * [ "seven" , 18 ]) tpl = "i am {name:s}, age {age:d}" . format (name = "seven" , age = 18 ) tpl = "i am {name:s}, age {age:d}" . format ( * * { "name" : "seven" , "age" : 18 }) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}" . format ( 15 , 15 , 15 , 15 , 15 , 15.87623 , 2 ) tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}" . format ( 15 , 15 , 15 , 15 , 15 , 15.87623 , 2 ) tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}" . format ( 15 ) tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}" . format (num = 15 ) |
更多格式化操作:https://docs.python.org/3/library/string.html
1、迭代器
迭代器是访问集合元素的一种方式。迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束。迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退。另外,迭代器的一大优点是不要求事先准备好整个迭代过程中所有的元素。迭代器仅仅在迭代到某个元素时才计算该元素,而在这之前或之后,元素可以不存在或者被销毁。这个特点使得它特别适合用于遍历一些巨大的或是无限的集合,比如几个G的文件
特点:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
>>> a = iter ([ 1 , 2 , 3 , 4 , 5 ]) >>> a <list_iterator object at 0x101402630 > >>> a.__next__() 1 >>> a.__next__() 2 >>> a.__next__() 3 >>> a.__next__() 4 >>> a.__next__() 5 >>> a.__next__() Traceback (most recent call last): File "<stdin>" , line 1 , in <module> StopIteration |
2、生成器
一个函数调用时返回一个迭代器,那这个函数就叫做生成器(generator);如果函数中包含yield语法,那这个函数就会变成生成器;
1
2
3
4
5
|
def func(): yield 1 yield 2 yield 3 yield 4 |
上述代码中:func是函数称为生成器,当执行此函数func()时会得到一个迭代器。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
>>> temp = func() >>> temp.__next__() 1 >>> temp.__next__() 2 >>> temp.__next__() 3 >>> temp.__next__() 4 >>> temp.__next__() Traceback (most recent call last): File "<stdin>" , line 1 , in <module> StopIteration |
3、实例
a、利用生成器自定义range
1
2
3
4
5
6
7
8
|
def nrange(num): temp = - 1 while True : temp = temp + 1 if temp > = num: return else : yield temp |
b、利用迭代器访问range
1
|
... |
标签:八进制 第一个 ack sig 包含 精度 border func 1.5
原文地址:http://www.cnblogs.com/ldh-linux/p/7054559.html