Python is generated using the principles Detailed
Original cow fortune was a big release on 2018-09-05 14:36:38
0.range () function, which function is to create a list of integers, generally used in a for loop
Syntax: range (start, stop, step), refer to the following parameters:
- * Start: start counting from the beginning. The default is zero. E.g. range (4) is equivalent to the range (0, 4); Results: (0,1,2,3) *
- * Stop: stop counting to the end, but not stop. For example: range (0, 5) is [0, 1, 2, 3, 4] no 5 *
step: the step size, the default is 1. For example: range (0, 5) is equivalent to the range (0, 5, 1)
#使用range函数建立列表 ls =[x*2 for x in range(10)] print(ls)#[0, 2, 4, 6, 8, 10, 12, 14, 16, 18] ls1 = [x for x in range(0,10,2)] #步长是2. print(ls1) #[0, 2, 4, 6, 8] ls2 = [x for x in range(3,10,2)] #开始从3开始,步长是2. print(ls2) # [3, 5, 7, 9] ls3 =[x for x in range(0, -10, -1)] #负数的使用 print(ls3) #[0, -1, -2, -3, -4, -5, -6, -7, -8, -9] print(range(0)) #range(0, 0) print(range(1,0)) #range(1, 0)
Creating the element 1. generator iterates over
1.1 Creating a builder methods: as long as a list of the formula [] into ()
* Generator (generator) is actually a special kind of iterator * . Every time we get blog earlier iteration of the data generated in accordance with specific rules (by next () method). But when we realize an iterator, on the state of the current iteration to the needs of our own record, and then only in accordance with the current state of the data generated. To reach the current state of the recording, and with the next () function iteration, Python will engage a builder . So * Generator (generator) is actually a special kind of iterator * .
#1.创建生成器 ls = [x*2 for x in range(10)] generator1 =(x*2 for x in range(10)) #这是一个生成器generator print(ls) print(generator1) #注意,打印生成器,不会像列表一样打印他的值,而是地址。 ''' [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] <generator object <genexpr> at 0x00000239FE00A620> '''
1.1 traversal content generator
`` `python
traverse Content Builder object:
1. A method for using the loop iterates
for I in generator1:
Print (I)
Method 2: Use the command line next () function: Next call (G), calculates the value of the next element of G, until the last element is calculated
When no more elements thrown StopIteration exception.
generator1 =(x2 for x in range(5))
next(generator1)
0
next(generator1)
2
next(generator1)
4
next(generator1)
6
next(generator1)
8
next(generator1)
Traceback (most recent call last):
File "", line 1, in For X in Range 2 (. 5))
StopIteration
2. The method 2.python script uses the next () method, the actual development is achieved by traversing the for loop, this next () method is too cumbersome.
g1 = (x
the while True:
the try:
X = Next (G1)
Print (X)
the except the StopIteration AS E:
Print ( "% S values =" e.Value%)
BREAK # Note that to add break, otherwise, an infinite loop.
'' 'Results are as follows:
0
2
. 4
. 6
. 8
values = None
' ''
3. Method 3: Use the object carrying the __next __ () method, the effect is equivalent to the next (g1) function
g1 = (x * 2 for x in Range (. 5))
G1 .__ Next __ ()
0
G1 .__ Next __ ()
2
G1 .__ Next __ ()
. 4
G1 .__ Next __ ()
. 6
G1 .__ Next __ ()
. 8
G1 .__ Next __ ()
Traceback (MOST Recent Call Last):
File "", line 1, in
StopIteration```
1.2 Creating generator Method 2: Create a function generator using the yield.
generator is very powerful. If the projection algorithm is complex, when a similar list with the formula for loop can not be achieved, it can also be implemented as a function. In short: as long as the yield keyword in the def called Builder
#著名的斐波拉契数列(Fibonacci):除第一个和第二个数外,任意一个数都可由前两个数相加得到
#1.举例:1, 1, 2, 3, 5, 8, 13, 21, 34, ...使用函数实现打印数列的任意前n项。
def fib(times): #times表示打印斐波拉契数列的前times位。
n = 0
a,b = 0,1
while n<times:
print(b)
a,b = b,a+b
n+=1
return 'done'
fib(10) #前10位:1 1 2 3 5 8 13 21 34 55
#2.将print(b)换成yield b,则函数会变成generator生成器。
#yield b功能是:每次执行到有yield的时候,会返回yield后面b的值给函数并且函数会暂停,直到下次调用或迭代终止;
def fib(times): #times表示打印斐波拉契数列的前times位。
n = 0
a,b = 0,1
while n<times:
yield b
a,b = b,a+b
n+=1
return 'done'
print(fib(10)) #<generator object fib at 0x000001659333A3B8>
3.对生成器进行迭代遍历元素
方法1:使用for循环
for x in fib(6):
print(x)
''''结果如下,发现如何生成器是函数的话,使用for遍历,无法获取函数的返回值。
1
1
2
3
5
8
'''
方法2:使用next()函数来遍历迭代,可以获取生成器函数的返回值。同理也可以使用自带的__next__()函数,效果一样
f = fib(6)
while True:
try: #因为不停调用next会报异常,所以要捕捉处理异常。
x = next(f) #注意这里不能直接写next(fib(6)),否则每次都是重复调用1
print(x)
except StopIteration as e:
print("生成器返回值:%s"%e.value)
break
'''结果如下:
1
1
2
3
5
8
生成器返回值:done
'''