小白教程
所有教程
关于
Search
172.70.100.224
172.70.100.224
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Python func next”的源代码
本页内容
上一节:
Python3_func_hex
下一节:
Python_func_slice
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:next()}}[[Category:python3 built in functions|9]] = Python next() 函数 = [[python built in functions|Python 内置函数]] == 描述 == '''next()''' 返回迭代器的下一个项目。 '''next()''' 函数要和生成迭代器的 '''iter()''' 函数一起使用。 == 语法 == next 语法: <sample title="" desc="" lang="python" hererun="1"> next(iterable[, default]) </sample> 参数说明: * iterable -- 可迭代对象 * default -- 可选,用于设置在没有下一个元素时返回该默认值,如果不设置,又没有下一个元素则会触发 StopIteration 异常。 == 返回值 == 返回下一个项目。 以下实例展示了 next 的使用方法: <sample title="" desc="" lang="python" hererun="1"> #!/usr/bin/python # -*- coding: UTF-8 -*- # 首先获得Iterator对象: it = iter([1, 2, 3, 4, 5]) # 循环: while True: try: # 获得下一个值: x = next(it) print(x) except StopIteration: # 遇到StopIteration就退出循环 break </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> 1 2 3 4 5 </sample> [[python built in functions|Python 内置函数]]
返回至“
Python func next
”。
上一节:
Python3_func_hex
下一节:
Python_func_slice