Discuss / Python / 今天生成器这里用了两个小时弄懂.

今天生成器这里用了两个小时弄懂.

Topic source

def triangles(): L=[1] while True : yield L
n=1
T=L[:] while n < len(T): L[n]=T[n-1]+T[n] n = n + 1 L.append(1) i = 0 for x in triangles(): print (x) i = i + 1 if i == 10: break

小笔记: 1.T=L是指T,L指向同一列表,而T=L[:]则是复制给T,千万不能搞混不然结果出错. 2.a,b = b, a+b是解包操作(教程中斐波那契那部分) 意思是:tmp = a a = b b = tmp +b 3.for x in triangles(): 这里是有序迭代triangles.

2.a,b = b, a+b是解包操作(教程中斐波那契那部分) 意思是:tmp = a a = b b = tmp +b

这个解决了我的疑惑,谢谢!

本木lxy

#3 Created at ... [Delete] [Delete and Lock User]

多谢!在这里困惑了好久


  • 1

Reply