next()

作者: import_hello | 来源:发表于2018-09-10 10:04 被阅读0次

转载须注明出处:简书@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.'

相关文章

网友评论

      本文标题:next()

      本文链接:https://www.haomeiwen.com/subject/sqgjgftx.html