杨辉三角如下:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
[1, 5, 10, 10, 5, 1]
实现一个生成器,不断输出下一行的列表,实现代码如下:
def triangles():
N = [1]
while True:
yield N
N.append(0)
N = [N[i-1] + N[i] for i in range(len(N))]
n = 0
results = []
for t in triangles():
print(t)
results.append(t)
n = n + 1
if n == 5:
break
print(results)
results.append(t)语句里的t和print(t)里的t应该一样呀,为何results中的列表末项会多一个零?