练习
杨辉三角定义如下:
1
/ \
1 1
/ \ / \
1 2 1
/ \ / \ / \
1 3 3 1
/ \ / \ / \ / \
1 4 6 4 1
/ \ / \ / \ / \ / \
1 5 10 10 5 1
把每一行看做一个list,试写一个generator,不断输出下一行的list:
def triangles():
L = [1]
while True:
yield L
L = [sum(i) for i in zip([0]+L,L+[0])]
其中,zip([0,a],[b,0]) = [(0,b),(a,0)] sum([0,a]) = 0+a