为什么迭代循环删除列表中的项目会停止 [英] Why iterative loop to remove items in list stops

查看:69
本文介绍了为什么迭代循环删除列表中的项目会停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的新手,试图了解这个旨在从列表中删除所有项目的迭代循环如何处理列表中的索引以及为什么它在其位置处停止...

为什么循环:

  foo = ['a','b','c','d','e','f','g','h']对于我在foo中:foo.remove(i)打印foo 

在这里停吗?

  ['b','d','f','h'] 

而不是这里?

  ['H'] 

还有,这里的索引在幕后"发生了什么?

在每次迭代中,Python是否都在同时跟踪下一个索引,所以一旦删除了一个项目,则该项目向右移动一个索引就向左移动(这就是为什么它跳过所有其他项目的原因)?

解决方案

要了解为什么会发生这种情况,请让我们逐步了解内部发生的情况.

第1步:

 >>>foo = ['a','b','c','d','e','f','g','h'] 

在这里,创建了一个新的列表对象,并将其分配给 foo .

第2步:

 >>>对于我在foo中: 

现在,迭代开始.在 index 0 处为 i 循环变量分配item的值,即'a'.

第3步:

 >>>foo.remove(i)>>>打印foo['b','c','d','e','f','g','h'] 

现在, .remove(i)执行 .remove(foo [0])而不是 .remove('a').现在,新列表在索引0处具有'b',在索引1处具有'c',依此类推.

第4步:

 >>>对于我在foo中: 

对于下一次迭代,为 i 循环变量分配了索引1处的项的值,该值当前为'c'.

第5步:

 >>>foo.remove(i)>>>打印foo['b','d','e','f','g','h'] 

现在这一次, .remove(i)执行 .remove(foo [1]),这会将'c'从列表中删除.现在,当前列表在索引0处具有'b',在索引1处具有'd',依此类推.

第6步:

 >>>对于我在foo中: 

在下一次迭代中,为 i 循环变量分配了索引2 处的项的值,该值当前为'e'.

第7步:

 >>>foo.remove(i)>>>打印foo['b','d','f','g','h'] 

现在这一次, .remove(i)执行 .remove(foo [2]),这会将'e'从列表中删除.类似地,项目的索引也按照上面的第5步进行更改.

第8步:

 >>>对于我在foo中: 

对于下一次迭代,为 i 循环变量分配了索引3 处的项目值,该值当前为'g'.

第9步:

 >>>foo.remove(i)>>>打印foo['b','d','f','h'] 

现在这一次, .remove(i)执行 .remove(foo [3]),这会将'g'从列表中删除

步骤10:

 >>>对于我在foo中: 

现在, i 应该指向 index 4 处的项目,但是由于原始列表已减少为4个元素,因此执行将在此处停止.

 >>>富['b','d','f','h'] 

以上是执行后的最终列表.

一些结论:

  • 在对其进行迭代时,切勿更改列表的长度.简单来说,在对原始列表进行迭代时,请勿修改原始列表.

  • 在列表中迭代执行 .remove()时,循环变量将使用索引而不是原始列表中的实际项目来引用列表项目.

New to Python, trying to understand how this iterative loop that is intended to remove all items form the list is handling the indexes in the list and why it stops where it does...

Why does this loop:

foo = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
for i in foo:
    foo.remove(i)
    print foo

Stop here?

['b', 'd', 'f', 'h']

Instead of here?

['H']

Also, what's happening "under the hood" with the indexes here?

With every iteration, is Python keeping track of which index is next while at the same time, once an item is removed the item to its right moves one index to the left (and that's why it's skipping every other item)?

解决方案

To understand why this is happening, let us look step-by-step what is happening internally.

Step 1:

>>> foo = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

Here, a new list object is created and is assigned to foo.

Step 2:

>>> for i in foo:

Now, the iteration starts. i loop variable is assigned the value of item at index 0 which is 'a'.

Step 3:

>>> foo.remove(i)
>>> print foo
['b', 'c', 'd', 'e', 'f', 'g', 'h']

Now, .remove(i) performs .remove(foo[0]) and not .remove('a') apparently. The new list now has 'b' at index 0, 'c' at index 1 and so on.

Step 4:

>>> for i in foo:

For the next iteration, i loop variable is assigned the value of item at index 1 which is currently 'c'.

Step 5:

>>> foo.remove(i)
>>> print foo
['b', 'd', 'e', 'f', 'g', 'h']

Now this time, .remove(i) performs .remove(foo[1]) which removes 'c' from the list. The current list now has 'b' at index 0, 'd' at index 1 and so on.

Step 6:

>>> for i in foo:

For the next iteration, i loop variable is assigned the value of item at index 2 which is currently 'e'.

Step 7:

>>> foo.remove(i)
>>> print foo
['b', 'd', 'f', 'g', 'h']

Now this time, .remove(i) performs .remove(foo[2]) which removes 'e' from the list. Similarly, the indices of the items gets changed as in step 5 above.

Step 8:

>>> for i in foo:

For the next iteration, i loop variable is assigned the value of item at index 3 which is currently 'g'.

Step 9:

>>> foo.remove(i)
>>> print foo
['b', 'd', 'f', 'h']

Now this time, .remove(i) performs .remove(foo[3]) which removes 'g' from the list.

Step 10:

>>> for i in foo:

Now, i should point to item at index 4 but since the original list has been reduced to 4 elements, the execution will stop here.

>>> foo
['b', 'd', 'f', 'h']

Above is the final list after execution.

SOME CONCLUSIONS:

  • NEVER CHANGE THE LENGTH OF LISTS WHILE ITERATING ON THEM. In simple words, don't modify the original list while performing iteration on it.

  • When performing .remove() in a list iteratively, the loop variable will refer to the list item using indexes and not by the actual items in the original list.

这篇关于为什么迭代循环删除列表中的项目会停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆