标签:
1 def triangels(): 2 """ 3 杨辉三角 4 """ 5 lst = [1] 6 n_count = 2 # 下一行列表长度 7 while True: 8 yield lst 9 lst_n = list(range(0 ,n_count)) 10 lst = [1] + [lst[i-1]+lst[i] for i,v in enumerate(lst_n) if i!=0 and i!=n_count-1] + [1] 11 n_count += 1
调用:
1 n = 0 2 while n < 10: 3 next(x) 4 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] # [1, 7, 21, 35, 35, 21, 7, 1] # [1, 8, 28, 56, 70, 56, 28, 8, 1] # [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
# ...
标签:
原文地址:http://www.cnblogs.com/seastar1989/p/5689063.html