Python iter()对列表做了什么?
我有以下代码:Python iter()对列表做了什么?,python,iterator,python-3.5,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))) 输出为: ['animal', 'dog', 'car', 'bmw', 'color', 'blue'] <list_iterator object at 0x7f2d98b7
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)))
输出为:
['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'}
[‘动物’、‘狗’、‘汽车’、‘宝马’、‘颜色’、‘蓝色’]
{'dog':'dog'、'car':'car'、'animal':'animal'、'color':'color'、'blue':'blue'、'bmw':'bmw'}
{'car':'bmw','color':'blue','animal':'dog'}
我不明白,为什么拉链与a_iter
的工作原理不同于a
。iter()
做什么,列表是可编辑的,那么为什么要使用iter()
?谁能给我举个好例子解释一下吗?我在谷歌上搜索了一下,但我还是不明白。iter()
对列表没有任何作用;列表
对象有一个\uuuuuuuuuuuuuuuuuuuuuuuuu
方法,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'
i = iter(l)
while True:
try:
x = next(i)
print(x)
except StopIteration:
break
请注意,再次调用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
zip()zip()
对它们调用iter()
。对于迭代器对象,例如a_iter
,iter(a_iter)
返回迭代器本身(毕竟它已经是迭代器):
迭代器是独立的对象,每个迭代器都有自己的索引:
>>> 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')
因此,当您使用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')
iter()函数返回迭代器的一个实例,我们可以对其进行迭代以逐个获取所有值。这是一个节省内存的函数,因为它只存储当前元素值。为l
返回迭代器对象。与它一起可以用于迭代l
的元素
守则:
for x in l: print(x)
相当于使用迭代器显式执行此代码:
>>> 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'
i = iter(l)
while True:
try:
x = next(i)
print(x)
except StopIteration:
break
请注意,迭代器也可以使用for循环进行遍历:
i = iter(l)
for x in i:
print(x)
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)]
迭代器是一种特殊类型的对象,它们是用来进行迭代的,因此它们速度很快,并且不会保存在内存中,它们主要用于循环中以提高速度,您始终可以通过list()
将迭代器转换为列表。感谢您的精彩解释!
>>> 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)]