Discuss / Python / 杨辉三角

杨辉三角

Topic source

def triangles(n): L=[1] for i in range(1,n+1): yield L L=[1]+[L[x-1]+L[x] for x in range(1,i)]+[1] for i in triangles(10): print(i)

高室长

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

把你这个去掉后面的两句,改为普通的生成器。 在CMD中,用next()获得返回值,只能不断的返回[1],我这边的情况是这样。

高室长

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

我看了一下,你的这个生成器,如果用next()获得返回值,应该写为next(triangles(n))。 而你最后用for循环的数为i,而不是n。


  • 1

Reply