-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_generator.py
More file actions
60 lines (54 loc) · 1.29 KB
/
04_generator.py
File metadata and controls
60 lines (54 loc) · 1.29 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 生成器 generator
''' 使用类似列表生成式的方式,将[]更换为(),来快速创建generator '''
g1 = (n * n for n in range(1, 4))
print(g1) # <generator object <genexpr> at 0x1011aaa98>
''' 使用yield语句来创建generator函数 '''
def fib(max):
n, a, b = 0, 0, 1
while n <= max:
yield b
a, b = b, a + b
n += 1
return 'Done'
''' 使用next来执行调用generator '''
f1 = fib(4)
print( next(f1) ) # 1
print( next(f1) ) # 1
print( next(f1) ) # 2
print( next(f1) ) # 3
print( next(f1) ) # 5
# print( next(f1) ) # StopIteration: Done
''' 使用迭代来执行generator '''
f2 = fib(4)
for n in f2:
print(n) # 1 1 2 3 5
''' 迭代执行generator时无法执行到return语句,改写成while循环 '''
f3 = fib(4)
while True:
try:
n = next(f3)
print(n) # 1 1 2 3 5
except StopIteration as e: # 捕获StopIteration错误
print(e.value) # 'Done'
break
''' 杨辉三角形实现 '''
def triangles():
L = [1]
while L:
yield L
L.insert(0, 0)
L.append(0)
L = [ L[i] + L[i + 1] for i in range( len(L) - 1 ) ]
n, t = 6, triangles()
while n > -1:
print(next(t))
n -= 1
'''
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
[1, 6, 15, 20, 15, 6, 1]
'''