我的 Python for 循环无限循环,但仅当 运行 不止一次

My Python for loop loops infinitely, but only when run more than once

我正在尝试制作一个简单的算法来为您计算帕斯卡三角形中的一行,如图所示。

因此,如果我想知道第五行的值,我只需向程序输入“repeat = 5”,程序就会输出“[1,5,10,10,5,1]”

而我正是使用 for 循环完成的。它有效!...如果你 运行 它一次,只要我实现一个简单的 for _ in range(value) 第一个 for 循环无限循环。我试过使用“While(条件)”和“While True”,但似乎没有任何效果。

start = [1] #This can be any of the "steps" of the pascal triangle
secondList = []
repeat = 2  # If repeat = 1 code runs fine

for _ in range(repeat):            # Without the for loop the code runs fine (But you have to manually update start.)
    print(f"First start: {start}") # Prints [1] for the first time, then [1,1]. Then it never prints again

    for idx,i in enumerate(start):

        if idx == 0:
            j = i + 0
            secondList.append(j)

        if idx == len(start)-1:
            j = i + 0
            secondList.append(j)

        else:
            j = i + start[idx+1]
            secondList.append(j)

        print(f"End of for loop, secondList: {secondList}") # Prints [1], then [1,1], then [1,1,1,2], then [1,1,1,2,2] and so on and so forth. The for loop should run once and then twice but it runs on forever.

    start = secondList # Sets the result as the start so that it can loop as many times as instructed.

print(secondList)

注意:没有循环的代码工作正常,例如,我设置 start = [1,3,3,1](第 3 行)和 repeat = 1 然后代码吐出 [1,4,6,4,1](这是下一行,第 4 行)

secondList = [] 移动到 for _ in range(repeat),然后再 for idx, i in ...

您的代码中的问题实际上是由在第一次迭代结束时调用的 start = secondList 引起的。在此之前,startsecondList 指向不同的对象。现在您将它们绑定到同一个对象。

在第二次迭代中,首先您没有清空临时 secondListsecondList.append() start 附加了一些东西,因为它们现在是同一个对象,这是一个更严重的问题!因此,每次迭代 for idx, i in enumerate(start) 时,列表 start 本身都会增长。所以这个 for 循环永远不会结束。

所以我的补救办法是重新分配一个空列表给secondList,以便(i)重新初始化secondList和(ii)切断link之间的link和 start.

start = [1]
repeat = 5
for _ in range(repeat):
    print(f"First start: {start}")
    secondList = []
    for idx,i in enumerate(start):
        if idx == 0:
            j = i + 0
            secondList.append(j)
        if idx == len(start)-1:
            j = i + 0
            secondList.append(j)
        else:
            j = i + start[idx+1]
            secondList.append(j)
        print(f"End of the (inner) for loop, secondList: {secondList}")
    start = secondList
print(secondList)

输出:

First start: [1]
End of the (inner) for loop, secondList: [1, 1]
First start: [1, 1]
End of the (inner) for loop, secondList: [1, 2]
End of the (inner) for loop, secondList: [1, 2, 1]
First start: [1, 2, 1]
End of the (inner) for loop, secondList: [1, 3]
End of the (inner) for loop, secondList: [1, 3, 3]
End of the (inner) for loop, secondList: [1, 3, 3, 1]
First start: [1, 3, 3, 1]
End of the (inner) for loop, secondList: [1, 4]
End of the (inner) for loop, secondList: [1, 4, 6]
End of the (inner) for loop, secondList: [1, 4, 6, 4]
End of the (inner) for loop, secondList: [1, 4, 6, 4, 1]
First start: [1, 4, 6, 4, 1]
End of the (inner) for loop, secondList: [1, 5]
End of the (inner) for loop, secondList: [1, 5, 10]
End of the (inner) for loop, secondList: [1, 5, 10, 10]
End of the (inner) for loop, secondList: [1, 5, 10, 10, 5]
End of the (inner) for loop, secondList: [1, 5, 10, 10, 5, 1]
[1, 5, 10, 10, 5, 1]