[英]My Python for loop loops infinitely, but only when run more than once
I'm trying to make a simple algorithm that calculates a row in a pascal triangle for you, as seen in this image.我正在尝试制作一个简单的算法,为您计算帕斯卡三角形中的一行,如图所示。
So if I wanted to know the values of the fifth row, I just input "repeat = 5" to the program, and the program spits out "[1,5,10,10,5,1]"所以如果我想知道第五行的值,我只需在程序中输入“repeat = 5”,程序就会吐出“[1,5,10,10,5,1]”
And I did just that using a for loop.而我正是使用 for 循环做到了这一点。 And it works..., If you run it once, as soon as I implement a simple
for _ in range(value)
the first for loop loops infinitely.它可以工作......,如果你运行它一次,只要我
for _ in range(value)
第一个 for 循环就会无限循环。 I've tried using "While (condition)" and "While True" with a break, but nothing seems to work.我尝试过使用“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)
Note: The code without looping works fine, say for example, I set start = [1,3,3,1]
(Which is row 3) and repeat = 1
then the code spits out [1,4,6,4,1]
(Which is the next row, row 4)注意:没有循环的代码可以正常工作,例如,我设置
start = [1,3,3,1]
(这是第 3 行)和repeat = 1
然后代码吐出[1,4,6,4,1]
(这是下一行,第 4 行)
Move secondList = []
into for _ in range(repeat)
, before for idx, i in...
.将
secondList = []
移动到for _ in range(repeat)
之前for idx, i in...
。
The problem in your code actually results from start = secondList
which is invoked at the end of the first iteration.您的代码中的问题实际上是由在第一次迭代结束时调用的
start = secondList
引起的。 Before that, start
and secondList
pointed to different objects.在此之前,
start
和secondList
指向不同的对象。 Now you are binding them to the same object.现在您将它们绑定到同一个 object。
At the second iteration, first you didn't empty the temporary secondList
.在第二次迭代中,首先您没有清空临时的
secondList
。 A more serious problem arises from the fact that secondList.append()
also appends something to start
, since they are now the same object!一个更严重的问题是
secondList.append()
还附加了一些东西到start
,因为它们现在是同一个对象! So each time you iterate for idx, i in enumerate(start)
, the list start
itself grows.因此,每次迭代
for idx, i in enumerate(start)
时,列表start
本身就会增长。 So this for
loop can never end.所以这个
for
循环永远不会结束。
So my remedy is to re-assign an empty list to secondList
, in order to (i) re-initialize secondList
and (ii) sever the link between this and start
.所以我的补救措施是重新分配一个空列表给
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: 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.