>>>defy_():...L=[1]...whileTrue:...yieldL...L=L.append(0)...L=L.insert(0,0)...L=[L[x]+L[x+1]forxinrange(len(L))]...>>>next(y_())[1]>>>next(y_())[1]如上的一个generator是想...
>>> def y_():
... L=[1]
... while True:
... yield L
... L=L.append(0)
... L=L.insert(0,0)
... L=[L[x]+L[x+1] for x in range(len(L))]
...
>>> next(y_())
[1]
>>> next(y_())
[1]
如上的一个generator是想输出杨辉三角,结果一直输出[1]。。。希望有高手能指出我的问题。非常感谢
第二次:
>>> def y_():
... L=[1]
... while True:
... yield L
... L.append(0)
... L.insert(0,0)
... L=[L[x]+L[x+1] for x in L]
... return(L)
...
>>> n=0
>>> for t in y_():
... print(t)
... n+=1
... if n==10:
... break
...
[1]
[1, 1, 1]
[1, 2, 2, 2, 1]
[1, 3, 4, 4, 4, 3, 1]
[1, 4, 8, 8, 8, 8, 8, 4, 1]
[1, 5, 16, 5, 5, 5, 5, 5, 16, 5, 1]
Traceback (most recent call last):
File "", line 1, in
File "", line 7, in y_
File "", line 7, in
IndexError: list index out of range
>>> next(y_())
[1]
>>> next(y_())
[1]
展开