Discuss / Python / Question一个列表生成式同步两个循环

Question一个列表生成式同步两个循环

Topic source

Most of learners used a nested range-for. But I think, there should be something easier to make it, since it's usual to add up members in two list, like: [22,33,44] + [1,1,1,] and get [23,34,45] I've thought one like this: r = [1] + [x+y for x,y in f,b ] But it's not right.

# -*-coding: utf-8 -*-

def tri():
    f = [1,]
    yield(f)
    while(True):
        b = f[1:(len(f))] + [0,]
        r = [1] + [x+y for x,y in f,b  ]    //!
        f = r
        yield(f)
n = 0
for t in tri():
    print(t)
    n = n + 1
    if n == 4:
        break

乃木坂骑

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

nice Chinglish


  • 1

Reply