python之generator实现杨辉三角

杨辉三角定义如下:

      1
     / \
    1   1
   / \ / \
  1   2   1
 / \ / \ / \
1   3   3   1
/  \ / \ / \ / \
1  4   6   4   1
/ \ / \ / \ / \ / \
1  5   10  10  5   1

def triangles():
… L = [1]
… while True:
… yield L
… L = L + [0] #L.append(0)
… L = [(L[i-1]+L[i]) for i in range(len(L))]

n = 0
results = []
for t in triangles():
print(t)
results.append(t)
n = n + 1
if n == 10:
break

    原文作者:杨辉三角问题
    原文地址: https://blog.csdn.net/bro_two/article/details/81252697
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞