def triangles():
list=[1]
while True:
yield list #generator函数与普通函数的差别:在执行过程中,遇到yield就中断,下次又继续执行
list = [list[i - 1] + list[i] for i in range(len(list))] #核心代吗:
list.insert(0,1) #在开头添加1
list.append(1) #在后面添加1
if __name__=='__main__':
n=0
for t in triangles():
print(t) #打印
n=n+1
if n==10: #执行10次
break
```
结果
```
[1]
[1, 2, 1]
[1, 2, 3, 3, 1]
[1, 2, 3, 5, 6, 4, 1]
[1, 2, 3, 5, 8, 11, 10, 5, 1]
[1, 2, 3, 5, 8, 13, 19, 21, 15, 6, 1]
[1, 2, 3, 5, 8, 13, 21, 32, 40, 36, 21, 7, 1]
[1, 2, 3, 5, 8, 13, 21, 34, 53, 72, 76, 57, 28, 8, 1]
[1, 2, 3, 5, 8, 13, 21, 34, 55, 87, 125, 148, 133, 85, 36, 9, 1]
[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 142, 212, 273, 281, 218, 121, 45, 10, 1]
```