如果传入第二个参数, 获取最后一个元素之后, 下一次next返回该默认值, 而不会抛出 StopIteration:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
it = iter([1, 2, 5, 4, 3])
while True:
x = next(it, 'a')
print(x)
if x == 'a':
break
输入结果为:
1
2
5
4
3
aOut Of Bounds Out Of Bounds
915***[email protected]
7年前 (2019-03-26)