Python 3 内置函数 - iter() 函数
0. iter() 函数
生成迭代器。
1. 使用方法
>>> help(iter)
Help on built-in function iter in module builtins:
iter(...)
iter(iterable) -> iterator
iter(callable, sentinel) -> iterator
Get an iterator from an object. In the first form, the argument must
supply its own iterator, or be a sequence.
In the second form, the callable is called until it returns the sentinel.
2. 使用示例
示例1.
next() 返回下一个迭代器对象.
>>> a = [0,1,2,3,4]
>>> a = iter(a)
>>> next(a), next(a), next(a)
(0, 1, 2)
示例2.
>>> a = [0,1,2,3,4]
>>> a = iter(a)
>>>
>>> for i in a:
>>> print(i)
0
1
2
3
4
|