[英]how does "for" loop works python?
Why does it happen, that on four while-iteration "for loop" iterates only 2 times, while the array length is 3为什么会发生,在四次 while 迭代中“for 循环”仅迭代 2 次,而数组长度为 3
import time
array = [[1, 1, 3], [2, 2, 3], [3, 3, 3]]
while True:
#print (array)
time.sleep(1)
index = 0
print (array)
for proxy in array:
print("iteration")
if proxy[2] == 0:
del array[index]
continue
proxy[2] -= 1
index += 1
print ("\n")
When the last item of any of the arrays turns 0 (which happens after a few turns), you have the item deleted.当任何数组的最后一项变为 0 时(在几圈后发生),您就删除了该项。 This makes there only be 2 items instead of 3, and the for loop runs over those items, so it only iterates twice.这使得只有 2 个项目而不是 3 个,并且 for 循环遍历这些项目,因此它只迭代两次。
Edit::one good solution is to make a copy of the array, so that you can use one to delete items from, and one to iterate (loop) through编辑::一个好的解决方案是制作数组的副本,以便您可以使用一个来删除项目,并使用一个来迭代(循环)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.