Discuss / Python / 参考战鹰二段理解了 感谢 错位相加非常牛逼

### 参考战鹰二段理解了 感谢 错位相加非常牛逼

Topic source

#### 家有憨兔

#1 Created at ... [Delete] [Delete and Lock User]
``````def triangles():
L = [1]
yield L
while True:
L = [v+m for v, m in zip([0]+L, L+[0])]
yield L
``````

#### sy107@CHINA

#2 Created at ... [Delete] [Delete and Lock User]
``````#贴个完整的测试代码，借贵宝地
def triangles():    li=[1]    yield li    while True:        li=[v+n for v, n in zip([0]+li,li+[0])]        yield lin = 0results = []for t in triangles():    results.append(t)    n = n + 1    if n == 10:        breakfor t in results:    print(t)if results == [    [1],    [1, 1],    [1, 2, 1],    [1, 3, 3, 1],    [1, 4, 6, 4, 1],    [1, 5, 10, 10, 5, 1],    [1, 6, 15, 20, 15, 6, 1],    [1, 7, 21, 35, 35, 21, 7, 1],    [1, 8, 28, 56, 70, 56, 28, 8, 1],    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]:    print('测试通过!')else:    print('测试失败!')
``````

#### sy107@CHINA

#3 Created at ... [Delete] [Delete and Lock User]
``````def triangles():    li=[1]    yield li    while True:        li=[v+n for v, n in zip([0]+li,li+[0])]        yield lin = 0results = []for t in triangles():    results.append(t)    n = n + 1    if n == 10:        breakfor t in results:    print(t)if results == [    [1],    [1, 1],    [1, 2, 1],    [1, 3, 3, 1],    [1, 4, 6, 4, 1],    [1, 5, 10, 10, 5, 1],    [1, 6, 15, 20, 15, 6, 1],    [1, 7, 21, 35, 35, 21, 7, 1],    [1, 8, 28, 56, 70, 56, 28, 8, 1],    [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]]:    print('测试通过!')else:    print('测试失败!')
``````

#### sy107@CHINA

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

def triangles():

li=[1]

yield li

while True:

li=[v+n for v, n in zip([0]+li,li+[0])]

yield li

n = 0

results = []

for t in triangles():

results.append(t)

n = n + 1

if n == 10:

break

for t in results:

print(t)

if results == [

[1],

[1, 1],

[1, 2, 1],

[1, 3, 3, 1],

[1, 4, 6, 4, 1],

[1, 5, 10, 10, 5, 1],

[1, 6, 15, 20, 15, 6, 1],

[1, 7, 21, 35, 35, 21, 7, 1],

[1, 8, 28, 56, 70, 56, 28, 8, 1],

[1, 9, 36, 84, 126, 126, 84, 36, 9, 1]

]:

print('测试通过!')

else:

print('测试失败!')

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

``````def triangles():
L = [1]
while True:
yield L
L = [v + m for v, m in zip([0] + L, L + [0])]
``````

• 1