繁体   English   中英

如何在python的while循环语句中使用迭代器

[英]how to use iterator in while loop statement in python

是否可以在 Python 的 while 循环中使用生成器或迭代器? 例如,类似于:

i = iter(range(10))
while next(i):
    # your code

这样做的目的是将迭代构建到 while 循环语句中,使其类似于 for 循环,不同之处在于您现在可以在 while 语句中添加额外的逻辑:

i = iter(range(10))
while next(i) and {some other logic}:
    # your code

然后它成为一个很好的 for 循环/while 循环混合。

有谁知道如何做到这一点?

在 Python < 3.8 中,您可以使用itertools.takewhile

from itertools import takewhile

i = iter(range(10))
for x in takewhile({some logic}, i):
    # do stuff

这里的“一些逻辑”将是一个 1-arg 可调用的接收next(i)产生的任何内容:

for x in takewhile(lambda e: 5 > e, i):
    print(x)
0
1
2
3
4

在 Python >= 3.8 中,您可以使用赋值表达式执行以下操作:

i = iter(range(10))
while (x := next(i, None)) is not None and x < 5:
    print(x)

while next(i):有两个问题while next(i):

  1. for循环不同, while循环不会捕获在没有next值时引发的StopIteration异常; 在这种情况下,您可以使用next(i, None)返回“falsey”值,但是只要迭代器返回实际的 falsey 值, while循环也会停止
  2. next返回的值将被消耗并且不再在循环体中可用。 (在 Python 3.8+ 中,这可以通过赋值表达式解决,请参阅其他答案。)

相反,您可以将for循环与itertools.takewhile ,从可迭代对象或任何其他条件测试当前元素。 这将循环直到迭代耗尽,或者条件评估为假。

from itertools import takewhile
i = iter(range(10))
r = 0
for x in takewhile(lambda x: r < 10, i):
    print("using", x)
    r += x
print("result", r)

输出:

using 0
...
using 4
result 10

您只需要安排您的迭代器在到期时返回一个类似 false 的值。 例如,如果我们反转range以使其倒计时为 0:

>>> i = iter(range(5, -1, -1))
>>> while val := next(i):
...     print('doing something here with value', val)
...

这将导致:

doing something here with value 5
doing something here with value 4
doing something here with value 3
doing something here with value 2
doing something here with value 1
a = iter(range(10))

try:
    next(a)
    while True:
        print(next(a))
except StopIteration:
    print("Stop iteration")

你可以做

    a = iter(range(10))

    try:
        a.next()
        while True and {True or False logic}:
            print("Bonjour")
            a.next()
    except StopIteration:
        print("a.next() Stop iteration")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM