Discuss / Python / 我去,两个小时终于搞定

我去,两个小时终于搞定

Topic source

陈蔺川

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

    L=[1]

    t=1

    while t<=10:

        yield L

        L=[L[m-1]+i for m,i in enumerate(L)]+[1]

        L[0]=1

        t=t+1

    return 'done'

陈蔺川

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

思路一直都是对的,之前弄错一个函数L.append(1)写成了L.append[1]一直报错,后来替换成L=[L[m-1]+i for m,i in enumerate(L)]+[1]成功才找到之前错误原因,, 不知道迭代索引的话这个复杂点

    L=[1]

    while True:

        yield L

        m=-1

        P=[]

        for i in L:

            o=L[m]+i

            m=m+1

            P.append(o)

        L=P+[1]

        L[0]=1

陈蔺川

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

换个思路把索引做range迭代.是这样

    L=[1]

    while True:

        yield L

        P=[]

        for i in range(len(L)):

            o=L[i-1]+L[i]

            P.append(o)

        L=P+[1]

        L[0]=1

再简化下

    L=[1]

    while True:

        yield L

        L=[L[i-1]+L[i] for i in range(len(L))]+[1]

        L[0]=1

再优化下就是楼上大佬们的代码了


  • 1

Reply