Python/ref func next
来自菜鸟教程
<languages />
Python next()函数
例
创建一个迭代器,并逐项打印项目:
mylist = iter(["apple", "banana", "cherry"])
x =
next(mylist)
print(x)
x = next(mylist)
print(x)
x = next(mylist)
print(x)
定义和用法
The
next()
函数返回迭代器中的下一项。
您可以添加默认的返回值,以在迭代结束时返回。
句法
next(iterable, default)
参数值
| 参数 | 描述 |
|---|---|
| iterable | 需要。一个可迭代的对象。 |
| default | 可选的。如果迭代器已结束,则返回默认值。 |
更多例子
例
当迭代器到达末尾时,返回一个默认值:
mylist = iter(["apple", "banana", "cherry"])
x =
next(mylist, "orange")
print(x)
x = next(mylist, "orange")
print(x)
x =
next(mylist, "orange")
print(x)
x = next(mylist, "orange")
print(x)