列出“怪癖”在Python中

发布于 2024-12-12 06:10:08 字数 232 浏览 0 评论 0原文

我在交互式解释器中尝试使用列表进行一些操作,我注意到这一点:

>>> list = range(1, 11)
>>> for i in list:
...     list.remove(i)
...
>>> list
[2, 4, 6, 8, 10]

任何人都可以解释为什么它留下偶数吗?现在这让我很困惑......非常感谢。

I was trying out some things with lists in the interactive interpreter and I noticed this:

>>> list = range(1, 11)
>>> for i in list:
...     list.remove(i)
...
>>> list
[2, 4, 6, 8, 10]

Can anyone explain why it left even numbers? This is confusing me right now... Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甚是思念 2024-12-19 06:10:08

修改正在迭代的列表是不安全的。

It isn't safe to modify a list that you are iterating over.

雨落星ぅ辰 2024-12-19 06:10:08

我的猜测是 for 循环的实现如下:

list = range(1, 11)

i = 0
while i < len(list):
    list.remove(list[i])
    i += 1

print(list)

每次删除一个元素时,“下一个”元素就会滑入其位置,但 i 无论如何都会递增,跳过 2 个元素。

但是,ObscureRobot 是对的,这样做并不安全(这可能是未定义的行为)。

My guess is that the for loop is implemented like the following:

list = range(1, 11)

i = 0
while i < len(list):
    list.remove(list[i])
    i += 1

print(list)

Every time an element is removed, the "next" element slides into its spot, but i gets incremented anyway, skipping 2 elements.

But yes, ObscureRobot is right, it's not really safe to do this (and this is probably undefined behavior).

口干舌燥 2024-12-19 06:10:08

如果要在迭代列表时修改列表,请从后到前进行操作:

lst = range(1, 11)
for i in reversed(lst):
    lst.remove(i)

If you want to modify a list whilst iterating over it, work from back to front:

lst = range(1, 11)
for i in reversed(lst):
    lst.remove(i)
半衾梦 2024-12-19 06:10:08

我发现使用 Python 解释这一点最简单:

>>> for iteration, i in enumerate(lst):
...     print 'Begin iteration', iteration, 'where lst =', str(lst), 'and the value at index', iteration, 'is', lst[iteration]
...     lst.remove(i)
...     print 'End iteration', iteration, 'where lst =', str(lst), 'with', i, 'removed\n'
... 
Begin iteration 0 where lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and the value at index 0 is 1
End iteration 0 where lst = [2, 3, 4, 5, 6, 7, 8, 9, 10] with 1 removed

Begin iteration 1 where lst = [2, 3, 4, 5, 6, 7, 8, 9, 10] and the value at index 1 is 3
End iteration 1 where lst = [2, 4, 5, 6, 7, 8, 9, 10] with 3 removed

Begin iteration 2 where lst = [2, 4, 5, 6, 7, 8, 9, 10] and the value at index 2 is 5
End iteration 2 where lst = [2, 4, 6, 7, 8, 9, 10] with 5 removed

Begin iteration 3 where lst = [2, 4, 6, 7, 8, 9, 10] and the value at index 3 is 7
End iteration 3 where lst = [2, 4, 6, 8, 9, 10] with 7 removed

Begin iteration 4 where lst = [2, 4, 6, 8, 9, 10] and the value at index 4 is 9
End iteration 4 where lst = [2, 4, 6, 8, 10] with 9 removed

请注意,(a) 在迭代列表时修改列表并 (b) 调用列表“列表”是一个坏主意。

I find this easiest to explain using Python:

>>> for iteration, i in enumerate(lst):
...     print 'Begin iteration', iteration, 'where lst =', str(lst), 'and the value at index', iteration, 'is', lst[iteration]
...     lst.remove(i)
...     print 'End iteration', iteration, 'where lst =', str(lst), 'with', i, 'removed\n'
... 
Begin iteration 0 where lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and the value at index 0 is 1
End iteration 0 where lst = [2, 3, 4, 5, 6, 7, 8, 9, 10] with 1 removed

Begin iteration 1 where lst = [2, 3, 4, 5, 6, 7, 8, 9, 10] and the value at index 1 is 3
End iteration 1 where lst = [2, 4, 5, 6, 7, 8, 9, 10] with 3 removed

Begin iteration 2 where lst = [2, 4, 5, 6, 7, 8, 9, 10] and the value at index 2 is 5
End iteration 2 where lst = [2, 4, 6, 7, 8, 9, 10] with 5 removed

Begin iteration 3 where lst = [2, 4, 6, 7, 8, 9, 10] and the value at index 3 is 7
End iteration 3 where lst = [2, 4, 6, 8, 9, 10] with 7 removed

Begin iteration 4 where lst = [2, 4, 6, 8, 9, 10] and the value at index 4 is 9
End iteration 4 where lst = [2, 4, 6, 8, 10] with 9 removed

Note that it's a bad idea to (a) modify a list while iterating over it and (b) call a list "list".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文