Discuss / Python / 之前不知道可以直接加号连接list,学到了

之前不知道可以直接加号连接list,学到了

Topic source

这段代码怎么让两边输出1的,实在想不懂,有大神帮助一下的吗

  L = [0] + L + [0]

抄作业抄作业

nb,nb 利用L=[0]+L+[0] 扩展list元素个数,省去append;然后巧用生成器,牛的一腿

优秀!

# -*- 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)]

青柠2638

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

大佬......

Seayon阿阳

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

看还得看半天才能看懂,膜拜牛人

除了卧草,我真想不出其他的回复了

胡敢40524

#19 Created at ... [Delete] [Delete and Lock User]
#写的有点复杂,还是贴出来,好好跟着大佬沾下光
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

singcao

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

厉害了,不过我觉得 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]

L=[0]+L+[0]是为了占位吗


Reply