Python中的无限for循环-列表是否已更新?

Python中的无限for循环-列表是否已更新?,python,python-3.x,loops,for-loop,infinite-loop,Python,Python 3.x,Loops,For Loop,Infinite Loop,首先,我知道我应该使用while循环来创建一个无限循环。然而,在Python中创建无限for循环时,我遇到了一些我不完全理解的问题 我对循环的“无限”的想法如下: l = [1] for el in l: print(len(l)) l.append(1) 这实际上创建了一个无限循环,因为在每次循环迭代中,1不断地被添加到列表l。所以我想我不希望我的列表变得越来越长,以至于在某个时候我没有足够的内存。所以我这样做了: l = [1] for el in l: print(len(l

首先,我知道我应该使用
while
循环来创建一个无限循环。然而,在Python中创建无限
for
循环时,我遇到了一些我不完全理解的问题

我对循环的“无限”
的想法如下:

l = [1]
for el in l:
  print(len(l))
  l.append(1)
这实际上创建了一个无限循环,因为在每次循环迭代中,
1
不断地被添加到列表
l
。所以我想我不希望我的列表变得越来越长,以至于在某个时候我没有足够的内存。所以我这样做了:

l = [1]
for el in l:
  print(len(l))
  l.pop(0)
  l.append(1)
我只进行了一次迭代。有人能解释一下原因吗?这是因为
l
仍然引用同一对象,并且其长度始终为1吗?

如果您使用如上所述的方法,则更易于实现。
下面的类实现迭代器类所需的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

class WhileLoop:

    def __init__(self, value):
        #Assign the value
        self.value = value

    def __iter__(self):
        #The iterator will be the class itself
        return self

    def __next__(self):
        #The next element in the loop is the value itself
        return self.value
然后,您可以调用并循环这个迭代器,如下所示

loop = WhileLoop(1)
for item in loop:
    print(item)
如果您使用上述建议的方法,则更容易实现。
下面的类实现迭代器类所需的
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

class WhileLoop:

    def __init__(self, value):
        #Assign the value
        self.value = value

    def __iter__(self):
        #The iterator will be the class itself
        return self

    def __next__(self):
        #The next element in the loop is the value itself
        return self.value
然后,您可以调用并循环这个迭代器,如下所示

loop = WhileLoop(1)
for item in loop:
    print(item)
返回一个“迭代器”,它是一个数据流的容器,每次返回一个元素,因此您不是在iterable(本例中是您的列表)上迭代,而是在该容器上迭代

next(it)
# Traceback (most recent call last):
#   File "main.py", line 16, in <module>
#     next(it)
# StopIteration
您可以使用
iter()
直接返回该迭代器,以便更好地了解for循环的情况

items = ['a']

it = iter(items)
print(it.__reduce__())
# (<built-in function iter>, (['a'],), 0)
现在我们看到索引值已更改为
1
,因为迭代器已返回列表中的第一个元素,在下一次迭代中,它将尝试返回列表中的第二个元素。如果尝试删除列表中的第一个元素,然后将另一个元素附加到列表中,则迭代器的结果状态如下

next(it)
print(it.__reduce__())
# (<built-in function iter>, (['a'],), 1)
items.pop(0)
items.append('b')
print(it.__reduce__())
# (<built-in function iter>, (['b'],), 1)
如果您真的对创建无限for循环感兴趣,那么使用生成器将是处理内存问题的更好方法(尽管正如您在问题中所指出的,没有太多好的理由不使用
while
)。请参阅。

返回一个“迭代器”,它是数据流的容器,每次返回一个元素,因此您不是在iterable(本例中是您的列表)上迭代,而是在该容器上迭代

next(it)
# Traceback (most recent call last):
#   File "main.py", line 16, in <module>
#     next(it)
# StopIteration
您可以使用
iter()
直接返回该迭代器,以便更好地了解for循环的情况

items = ['a']

it = iter(items)
print(it.__reduce__())
# (<built-in function iter>, (['a'],), 0)
现在我们看到索引值已更改为
1
,因为迭代器已返回列表中的第一个元素,在下一次迭代中,它将尝试返回列表中的第二个元素。如果尝试删除列表中的第一个元素,然后将另一个元素附加到列表中,则迭代器的结果状态如下

next(it)
print(it.__reduce__())
# (<built-in function iter>, (['a'],), 1)
items.pop(0)
items.append('b')
print(it.__reduce__())
# (<built-in function iter>, (['b'],), 1)

如果您真的对创建无限for循环感兴趣,那么使用生成器将是处理内存问题的更好方法(尽管正如您在问题中所指出的,没有太多好的理由不使用
while
)。请参阅。

,因为您正在添加和删除一个元素。也就是“+1-1=0”。列表的长度永远不会改变,因此您只能得到1次迭代,这是列表的初始长度。请查看迭代器协议,我也知道您对此发表了评论,但您不应该像这样使用
for
循环。出于毫无意义的原因不断地从内存中写入和弹出对计算机有害,因此您确实应该使用
while True
循环。这种类型的永久循环是非常糟糕的做法。因为您正在添加和删除一个元素。也就是“+1-1=0”。列表的长度永远不会改变,因此您只能得到1次迭代,这是列表的初始长度。请查看迭代器协议,我也知道您对此发表了评论,但您不应该像这样使用
for
循环。出于毫无意义的原因不断地从内存中写入和弹出对计算机有害,因此您确实应该使用
while True
循环。这种类型的永久循环是非常糟糕的做法。