Python内置函数(44)——next
2016-11-07 10:51
211 查看
英文文档:
2. 函数可以接收一个可选的default参数,传入default参数后,如果可迭代对象还有元素没有返回,则依次返回其元素值,如果所有元素已经返回,则返回default指定的默认值而不抛出
next(iterator[, default])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
StopIterationis raised.说明: 1. 函数必须接收一个可迭代对象参数,每次调用的时候,返回可迭代对象的下一个元素。如果所有元素均已经返回过,则抛出
StopIteration异常。
>>> a = iter('abcd') >>> next(a) 'a' >>> next(a) 'b' >>> next(a) 'c' >>> next(a) 'd' >>> next(a) Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> next(a) StopIteration
2. 函数可以接收一个可选的default参数,传入default参数后,如果可迭代对象还有元素没有返回,则依次返回其元素值,如果所有元素已经返回,则返回default指定的默认值而不抛出
StopIteration异常。
>>> a = iter('abcd') >>> next(a,'e') 'a' >>> next(a,'e') 'b' >>> next(a,'e') 'c' >>> next(a,'e') 'd' >>> next(a,'e') 'e' >>> next(a,'e') 'e'
相关文章推荐
- Python内置函数——next
- Python标准库:内置函数next(iterator[, default])
- Python标准库:内置函数next(iterator[, default])
- Python内置函数 next的具体使用方法
- Python标准库:内置函数next(iterator[, default])
- Python生成器generator之next和send运行流程
- 【Python】内置函数列表合集
- Python内置函数
- Python标准库:内置函数dict(**kwarg)
- python学习之内置函数
- Python内置函数(转)
- Python问题:AttributeError: '_csv.reader' object has no attribute 'next' 以及UnicodeDecodeError: 'gbk' co
- Python内置函数之len()
- Python标准库:内置函数hasattr(object, name)
- python3.x中的生成器generator调用next方法
- python函数: 内置函数
- Python函数篇(3)-内置函数、文件处理
- python 的几个内置函数(lambda ,zip filter )用法
- python内置函数
- Python 内置函数 lambda、filter、map、reduce