Discuss / Python / 错了好多次,终于弄对了......

错了好多次,终于弄对了......

Topic source
def triangles():
    l = [1]    
    yield l
    l = [1,1]    
    yield l
    while True:
        l1 = l[1:]
        l2 = l[:-1]
        temp = l1
        for n in range(len(temp)):
            temp[n]=l1[n]+l2[n]
        l = [1]+temp+[1]
        yield l

经过优化得到下面的代码

def triangles():
    l = [1]
    yield l
    l = [1,1]
    yield l    
    while True:        
        l1 = l[1:]        
        for n in range(len(l1)):
            l[n] = l1[n]+l[n]
        l = [1]+l
        yield l

Todayzhou

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

谢谢你,看你的代码,我也弄懂啦~~~~


  • 1

Reply