python - 在 for 循环期间修改可迭代的大小 - 循环是如何确定的?

标签 python mutation

two 中提到了 For 循环places在 python 文档中(我发现)。我确实尝试找到 for 的源代码循环 cpython但没有效果。

这就是我想要理解的:我假设 for 循环是一种 while i <= len(iterable) then loopif i <= len(iterable) then loop: 。我不确定情况是否如此,并且 here's为什么:

y = [1, 2, 3, 4]
for x in y:
  print(y)
  print(y.pop(0))

Output:
[1, 2, 3, 4]
1
[2, 3, 4]
2

知道在循环迭代时不应该修改它。我知道。但这仍然不是随机结果 - 每次运行此代码时都会发生:2 个循环。如果运行 pop(),您还会得到 2 个循环相反。

也许更好奇的是,您似乎可靠地得到 len(y)+1//2循环(至少使用 .pop() ,我没有尝试太多其他测试):

  • 如果 y = [1, 2]有一个循环
  • 如果 y = [1, 2, 3]有两个循环
  • 如果 y = [1, 2, 3, 4] 仍然有两个循环
  • 如果 y = [1, 2, 3, 4, 5]共有三个循环
  • 如果 y = [1, 2, 3, 4, 5, 6] 仍然三个循环
  • 如果 y = [1, 2, 3, 4, 5, 6, 7]四个循环

根据 Python 文档:

Note

There is a subtlety when the sequence is being modified by the loop (this can only occur for mutable sequences, e.g. lists). An internal counter is used to keep track of which item is used next, and this is incremented on each iteration. When this counter has reached the length of the sequence the loop terminates. This means that if the suite deletes the current (or a previous) item from the sequence, the next item will be skipped (since it gets the index of the current item which has already been treated). Likewise, if the suite inserts an item in the sequence before the current item, the current item will be treated again the next time through the loop. This can lead to nasty bugs that can be avoided by making a temporary copy using a slice of the whole sequence, e.g.,

for x in a[:]:
    if x < 0: a.remove(x)

任何人都可以解释Python在循环遍历循环期间修改的可迭代对象时使用的逻辑吗?怎么办iterStopIteration ,和__getitem__(i)IndexError考虑到?那么非列表的迭代器呢?最重要的是,这是/它在文档中的什么位置?

正如@Yang K建议的:

y = [1, 2, 3, 4, 5, 6, 7]
for x in y:
  print("y: {}, y.pop(0): {}".format(y, y.pop(0)))
  print("x: {}".format(x))

# Output
y: [2, 3, 4, 5, 6, 7], y.pop(0): 1
x: 1
y: [3, 4, 5, 6, 7], y.pop(0): 2
x: 3
y: [4, 5, 6, 7], y.pop(0): 3
x: 5
y: [5, 6, 7], y.pop(0): 4
x: 7

最佳答案

循环执行直到 iterable 表示它没有更多元素为止。两次循环后,iterable 已经遍历了两个元素,并且丢失了两个元素,这意味着它已到达末尾,循环终止。

您的代码相当于:

y = [1, 2, 3, 4]
i = iter(y)
while True:
    try:
        x=next(i)
    except StopIteration:
        break
    print(y)
    print(y.pop(0))

列表迭代器保存接下来要读取的索引。在第三个周期中,列表为[3, 4]next(i)需要读取y[2],这是不可能的,因此 next 引发 StopIteration,从而结束循环。

编辑至于您的其他问题:

How do iter and StopIteration, and __getitem__(i) and IndexError factor in?

前两个如上所述:它定义了 for 循环。或者,如果你愿意的话,它是 iter 的契约:它将产生一些东西,直到它以 StopIteration 停止。

后两者,我认为根本不参与,因为列表迭代器是 implemented in C ;例如,检查迭代器是否耗尽直接将当前索引与PyList_GET_SIZE进行比较,后者直接查看->ob_size字段;它不再通过Python。显然,您可以创建一个完全用纯 Python 编写的列表迭代器,并且您可能会使用 len 来执行检查,或者捕获 IndexError 并再次让底层 C 代码对 ->ob_size 执行检查。

What about iterators that aren't lists?

您可以将任何对象定义为可迭代的。当你调用iter(obj)时,它与调用obj.__iter__()是一样的。这预计会返回一个迭代器,它知道如何处理 i.__next__() (这就是 next(i) 翻译的内容)。我相信字典通过在其键列表中建立索引来迭代(我认为,尚未检查)。如果你编写代码,你可以创建一个可以做任何你想做的事情的迭代器。例如:

class AlwaysEmpty:
    def __iter__(self):
        return self
    def __next__(self):
        raise StopIteration

for x in AlwaysEmpty():
    print("there was something")

可以预见的是,不会打印任何内容。

And most importantly, is this / where is this in the docs?

Iterator Types

关于python - 在 for 循环期间修改可迭代的大小 - 循环是如何确定的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53331290/

相关文章:

python - 编写一个 fasttext 自定义转换器

python - 如何在 python 中删除行 CSV

interface - GraphQL:使用接口(interface)管理突变的最佳方法?

java - 用 ANN 求解 XOR 的进化算法的改进

Python Pandas 比较 CSV keyerror

类变量中的Python静态继承

python - 如何将 JSON 数据 PUT/POST 到 ListSerializer?

javascript - 从 contenteditable div 中获取已删除的节点(Froala Editor)

datatables - 如何使用GraphQL删除多个项目?

r - 在 R 中将特定字符串与 HGVS 格式分开