Manual
Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.
直译
通过调用迭代器的__next__()方法来检索它的下一项。如果给定default,当迭代尽时将返回这个值,否则引发StopIteration。
实例
>>> a = [1, 2, 3, 4, 5]
>>> a = iter([1, 2, 3, 4, 5])
>>> next(a)
1
>>> next(a)
2
>>> next(a)
3
···
本文介绍了如何使用Python中的迭代器来遍历元素,并解释了通过调用迭代器的__next__()方法来检索下一个项的过程。此外,还展示了如何处理迭代完成的情况,即如何设定默认返回值或引发StopIteration异常。
2365

被折叠的 条评论
为什么被折叠?



