Python 中的迭代器
Python 中的迭代器
Python 3中的迭代器
容器与迭代器
在Python 3中,支持迭代器的容器,只要支持__iter__()方法获取一个迭代器对象既可。
我们来看几个例子.
列表迭代器
首先是列表:
>>> a = [1,2,3,4]
>>> a
[1, 2, 3, 4]
>>> type(a)
<class 'list'>
我们通过调用list的__iter__()方法来获取list的iterator:
>>> a2 = a.__iter__()
>>> a2
<list_iterator object at 0x1068d1630>
元组迭代器
元组有自己的迭代器tuple_iterator
>>> c =