[英]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
引起的。 在此之前, start
和secondList
指向不同的对象。 现在您将它们绑定到同一个 object。
在第二次迭代中,首先您没有清空临时的secondList
。 一个更严重的问题是secondList.append()
还附加了一些东西到start
,因为它们现在是同一个对象! 因此,每次迭代for idx, i in enumerate(start)
时,列表start
本身就会增长。 所以这个for
循环永远不会结束。
所以我的补救措施是重新分配一个空列表给secondList
,以便(i)重新初始化secondList
和(ii)切断 this 和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)
Output:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.