dor*_*and 2 python iterator python-3.5
我有这个代码:
a = ['animal', 'dog', 'car', 'bmw', 'color', 'blue']
a_iter = iter(a)
print(a)
print(a_iter)
print(dict(zip(a,a)))
print(dict(zip(a_iter,a_iter)))
Run Code Online (Sandbox Code Playgroud)
输出是:
['animal', 'dog', 'car', 'bmw', 'color', 'blue']
<list_iterator object at 0x7f2d98b756d8>
{'dog': 'dog', 'car': 'car', 'animal': 'animal', 'color': 'color', 'blue': 'blue', 'bmw': 'bmw'}
{'car': 'bmw', 'color': 'blue', 'animal': 'dog'}
Run Code Online (Sandbox Code Playgroud)
我不understad,为什么拉链用不同的方式工作a_iter比a.做iter()什么,列表是可迭代的,为什么要使用iter()?有人可以用一些很好的例子向我解释这个吗?我用Google搜索,但我仍然不明白.
iter()对清单没有任何意义; 的list对象有一个__iter__该方法iter()使用,以产生迭代器对象.该对象具有对原始列表和索引的引用; 每次请求迭代器中的下一个值时,都会检索并返回当前索引处的值,并且索引会递增.
您可以使用该next()函数从迭代器获取下一个值:
>>> a = ['animal', 'dog', 'car', 'bmw', 'color', 'blue']
>>> a_iter = iter(a)
>>> next(a_iter) # get the next value
'animal'
>>> next(a_iter) # get the next value
'dog'
Run Code Online (Sandbox Code Playgroud)
请注意next()再次调用如何为您提供新值.你可以这样做,直到迭代器完成:
>>> three_more = next(a_iter), next(a_iter), next(a_iter)
>>> next(a_iter) # last one
'blue'
>>> next(a_iter) # nothing left
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
Run Code Online (Sandbox Code Playgroud)
列表迭代器对象保持原始列表对象; 更改列表对象将反映在生成的迭代器值上next():
>>> b = ['foo', 'bar']
>>> b_iter = iter(b)
>>> next(b_iter)
'foo'
>>> b[1] = 'spam'
>>> b
['foo', 'spam']
>>> next(b_iter)
'spam'
Run Code Online (Sandbox Code Playgroud)
zip()请求每个参数中的下一个值,假设它们是迭代的 ; zip()呼吁iter()他们.对于迭代器对象,例如a_iter,iter(a_iter)返回迭代器本身(毕竟它已经是迭代器):
>>> iter(a_iter)
<list_iterator object at 0x10e7b6a20>
>>> iter(a_iter) is a_iter
True
Run Code Online (Sandbox Code Playgroud)
因为a_iter将从原始列表中按顺序产生值,这意味着您在字典中获得了配对元素,因为它zip()具有对同一对象的两个引用 ; 您有效地创建(next(a_iter), next(a_iter))为迭代器步骤值zip().a另一方面,如果传入两个引用,zip()则将调用iter() 两次,创建两个单独的迭代器对象,并且每个对象都有自己的索引来跟踪.
让我们详细看一下.请注意,zip() 还产生一个迭代器对象,所以我们可以验证调用next()上zip()又造成a_iter挺身而出两次:
>>> a_iter = iter(a)
>>> a_iter_zip = zip(a_iter, a_iter)
>>> a_iter_zip # a zip object is an iterator too
<zip object at 0x10e7ba8c8>
>>> next(a_iter_zip) # get next value of a_iter, together with the next value of a_iter
('animal', 'dog')
>>> next(a_iter) # the a-list iterator was advanced, so now we get 'car'
'car'
>>> next(a_iter_zip) # now a_iter is at bmw, so we get bmw and color
('bmw', 'color')
Run Code Online (Sandbox Code Playgroud)
迭代器是独立的对象,它们每个都有自己的索引:
>>> a_iter1 = iter(a)
>>> a_iter2 = iter(a) # different iterator from a_iter1
>>> next(a_iter1), next(a_iter1) # what zip() does
('animal', 'dog')
>>> next(a_iter2), next(a_iter2) # iter2 is independent
('animal', 'dog')
Run Code Online (Sandbox Code Playgroud)
所以当你使用时zip(a, a),真正发生的是zip()调用iter(a)两次,创建两个新的迭代器,并且两者都用于创建输出:
>>> a_iter1 = iter(a)
>>> a_iter2 = iter(a)
>>> a_iter_1_and_2_zip = zip(a_iter1, a_iter2)
>>> next(a_iter_1_and_2_zip) # values from a_iter1 and a_iter2
('animal', 'animal')
>>> next(a_iter_1_and_2_zip) # moving in lockstep
('dog', 'dog')
>>> next(a_iter1) # moving one of these two one step along, to 'car'
'car'
>>> next(a_iter_1_and_2_zip) # so a_iter1 is one step ahead!
('bmw', 'car')
>>> next(a_iter1) # another extra step
'color'
>>> next(a_iter_1_and_2_zip) # so a_iter1 is two steps ahead!
('blue', 'bmw')
Run Code Online (Sandbox Code Playgroud)
iter(l)返回 的迭代器对象l。与next(i)它一起可用于迭代 的元素l。
代码:
for x in l: print(x)
Run Code Online (Sandbox Code Playgroud)
相当于显式使用迭代器的此代码:
i = iter(l)
while True:
try:
x = next(i)
print(x)
except StopIteration:
break
Run Code Online (Sandbox Code Playgroud)
请注意,迭代器也可以使用 for 循环来遍历:
i = iter(l)
for x in i:
print(x)
Run Code Online (Sandbox Code Playgroud)
zip(a,b)一次消耗a,中的一个元素。b
当 zip 的参数是序列时,它将为其创建自己的迭代器。
当参数是迭代器时,它只会消耗其中的元素。
当两个参数中的迭代器相同时,zip 的每次迭代都会为第一个参数消耗迭代器的一个元素,为第二个参数消耗迭代器的一个元素。
>>> a = [1,2,3,4]
>>> b = [10,20,30,40]
>>> list(zip(a, b)) # zip two lists
[(1, 10), (2, 20), (3, 30), (4, 40)]
>>> list(zip(a, a)) # zip a list with itself
[(1, 1), (2, 2), (3, 3), (4, 4)]
>>> i1 = iter(a)
>>> i2 = iter(a)
>>> list(zip(i1, i2)) # same as above, but with iterators
[(1, 1), (2, 2), (3, 3), (4, 4)]
>>> i = iter(a)
>>> list(zip(i, i)) # same as above, but with the same iterator
[(1, 2), (3, 4)]
Run Code Online (Sandbox Code Playgroud)