之前不知道可以直接加号连接list,学到了
Topic source优秀!
# -*- coding: utf-8 -*-
def triangles():
L = [1]
while 1:
yield L
L = [0] + L + [0]
L = [L[i] + L[i + 1] for i in range(len(L) - 1)]
#写的有点复杂,还是贴出来,好好跟着大佬沾下光
def triangles(): prev=0 next=0 results =[1] while(1): yield results temps=[] for i in range(len(results)): if(i==0): temps.append(prev+results[i]) if (i > 0 and i <= (len(results) - 1)): temps.append(results[i - 1] + results[i]) if(i==(len(results)-1)): temps.append(next+results[i]) results=temps
厉害了,不过我觉得 L = [0] + L + [0]可以和下面的合在一起吧
def triangles():
L = [1]
while True:
yield L
L = [1] + [L[i]+L[i+1] for i in range(len(L)-1)] + [1]
流苏tassels丶
这段代码怎么让两边输出1的,实在想不懂,有大神帮助一下的吗