next()

2018-09-10  本文已影响0人  import_hello

转载须注明出处:简书@Orca_J35

next(iterator[, default])

通过调用迭代器对象 iterator__next__() 方法来检索迭代器中的下一项。

如果已传入第二参数 default ,当迭代器耗尽时便会返回 default;如果没有传入第二参数,当迭代器耗尽时,则会抛出 StopIteration

示例 1:

>>> i = iter([1, 2])
>>> next(i)
1
>>> next(i)
2
>>> next(i)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
StopIteration

示例 2:

>>> i = iter([1, 2])
>>> next(i, 'No more items in the list.')
1
>>> next(i, 'No more items in the list.')
2
>>> next(i, 'No more items in the list.')
'No more items in the list.'
上一篇 下一篇

猜你喜欢

热点阅读