更新时间:2023-12-04 21:37:10
继续假设你有这个列表:
Here is what's going on. Suppose you have this list:
['a', 'b', 'c', 'd']
您正在循环列表中的每个元素。假设你目前在索引位置1:
and you are looping over every element in the list. Suppose you are currently at index position 1:
['a', 'b', 'c', 'd']
^
|
index = 1
...并删除索引位置1的元素,给出你这样:
...and you remove the element at index position 1, giving you this:
['a', 'c', 'd']
^
|
index 1
删除该项后,其他项目向左滑动,给您这是:
After removing the item, the other items slide to the left, giving you this:
['a', 'c', 'd']
^
|
index 1
然后当循环再次运行时,循环将索引增加到2,给你这个:
Then when the loop runs again, the loop increments the index to 2, giving you this:
['a', 'c', 'd']
^
|
index = 2
看看你如何跳过'c'?课程是:永远不要从您正在循环的列表中删除一个元素。
See how you skipped over 'c'? The lesson is: never delete an element from a list that you are looping over.