简体   繁体   English

next() 是否从生成器中消除值?

[英]Does next() eliminate values from a generator?

I've written a generator that does nothing more or less than store a range from 0 to 10:我写了一个生成器,它只存储一个从 0 到 10 的范围:

result = (num for num in range(11))

When I want to print values, I can use next() :当我想打印值时,我可以使用next()

print(next(result))
[Out]: 0
print(next(result))
[Out]: 1
print(next(result))
[Out]: 2
print(next(result))
[Out]: 3
print(next(result))
[Out]: 4

If I then run a for loop on the generator, it runs on the values that I have not called next() on:如果我然后在生成器上运行一个for循环,它会在我没有调用next()的值上运行:

for value in result:
    print(value)
[Out]: 5
6
7
8
9
10

Has the generator eliminated the other values by acting on them with a next() function?生成器是否通过使用next() function 作用于其他值来消除其他值? I've tried to find some documentation on the functionality of next() and generators but haven't been successful.我试图找到一些关于next()和生成器功能的文档,但没有成功。

Actually this is can be implicitly deduced from next 's docs and by understanding the iterator protocol/contract :实际上,这可以从next文档中隐含地推断出来,并通过理解iterator协议/合同

next(iterator[, default])下一个(迭代器 [,默认值])
Retrieve the next item from the iterator by calling its next () method.通过调用其next () 方法从迭代器中检索下一项。 If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.如果给出默认值,则在迭代器耗尽时返回,否则引发StopIteration

Yes .的。 Using a generator's __next__ method retrieves and removes the next value from the generator.使用生成器的__next__方法从生成器中检索并删除下一个值。

tldr; tldr; yes是的

An iterator is essentially a value producer that yields successive values from its associated iterable object.迭代器本质上是一个值生产者,它从其关联的可迭代 object 中产生连续的值。 The built-in function next() is used to obtain the next value from in iterator.内置的 function next()用于从迭代器中获取下一个值。 Here is an example using the same list as above:这是使用与上述相同列表的示例:

>>> l = ['Sarah', 'Roark']
>>> itr = iter(l)
>>> itr
<list_iterator object at 0x100ba8950>
>>> next(itr)
'Sarah'
>>> next(itr)
'Roark'
>>> next(itr)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

In this example, l is an iterable list and itr is the associated iterator, obtained with iter().在这个例子中, l是一个可迭代列表, itr是关联的迭代器,通过 iter() 获得。 Each next(itr) call obtains the next value from itr .每个next(itr)调用都从itr获取下一个值。

Notice how an iterator retains its state internally.请注意迭代器如何在内部保留其 state。 It knows which values have been obtained already, so when you call next() , it knows what value to return next.它知道已经获得了哪些值,因此当您调用next()时,它知道接下来要返回什么值。

If all the values from an iterator have been returned already, a subsequent next() call raises a StopIteration exception.如果已经返回了来自迭代器的所有值,则后续的next()调用会引发StopIteration异常。 Any further attempts to obtain values from the iterator will fail.任何进一步尝试从迭代器获取值都将失败。

We can only obtain values from an iterator in one direction.我们只能从一个方向的迭代器中获取值。 We can't go backward.我们不能 go 向后退。 There is no prev() function.没有prev() function。 But we can define two independent iterators on the same iterable object:但是我们可以在同一个可迭代的 object 上定义两个独立的迭代器:

>>> l
['Sarah', 'Roark', 30]
>>> itr1 = iter(l)
>>> itr2 = iter(l)
>>> next(itr1)
'Sarah'
>>> next(itr1)
'Roark'
>>> next(itr1)
30
>>> next(itr2)
'Sarah'

Yes, a for loop in Python just returns the next item from the iterator, the same way that next() does.是的,Python 中的 for 循环只是从迭代器返回下一项,与next()的方式相同。

https://docs.python.org/3/reference/compound_stmts.html#the-for-statement https://docs.python.org/3/reference/compound_stmts.html#the-for-statement

The suite is then executed once for each item provided by the iterator, in the order returned by the iterator.然后,该套件按照迭代器返回的顺序对迭代器提供的每个项目执行一次。

So you can think of a for loop like this:所以你可以想到这样的 for 循环:

for x in container:
    statement()

As (almost) equivalent to a while loop:作为(几乎)相当于一个while循环:

iterator = iter(container)
while True:
    x = next(iterator)
    if x is None:
        break
    statement()

If container is already an iterator, then iter(container) is container .如果container已经是一个迭代器,那么iter(container)就是container

Note: Technically, a for loop is more like this:注意:从技术上讲,for 循环更像这样:

iterator = iter(container)
while True:
    try:
        x = iterator.__next__()
    except StopIteration:
        break
    statement()

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

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