在 Python 3 中迭代字典 items()、values()、keys()

2022-01-10 00:00:00 python python-3.x dictionary iterator

问题描述

如果我理解正确,在 Python 2 中,iter(d.keys())d.iterkeys() 相同.但是现在,d.keys() 是一个视图,它位于列表和迭代器之间.视图和迭代器有什么区别?

If I understand correctly, in Python 2, iter(d.keys()) was the same as d.iterkeys(). But now, d.keys() is a view, which is in between the list and the iterator. What's the difference between a view and an iterator?

也就是说,在 Python 3 中,有什么区别

In other words, in Python 3, what's the difference between

for k in d.keys()
    f(k)

for k in iter(d.keys())
    f(k)

此外,这些差异如何在一个简单的 for 循环中显示出来(如果有的话)?

Also, how do these differences show up in a simple for loop (if at all)?


解决方案

我不确定这是否能很好地回答你的问题,但希望它能解释一下 Python 2 和 3 之间的区别尊重.

在 Python 2 中,iter(d.keys())d.iterkeys() 并不完全等效,尽管它们的行为相同.在第一个中,keys() 将返回字典的键列表的副本,然后 iter 将在此列表上返回一个迭代器对象,第二个是永远不会构建完整的键列表.

In Python 2, iter(d.keys()) and d.iterkeys() are not quite equivalent, although they will behave the same. In the first, keys() will return a copy of the dictionary's list of keys and iter will then return an iterator object over this list, with the second a copy of the full list of keys is never built.

d.keys() 在 Python 3 中返回的视图对象是 iterable (即可以从它们中生成迭代器)所以当你说 fork in d.keys() Python 将为您创建迭代器.因此,您的两个示例的行为将相同.

The view objects returned by d.keys() in Python 3 are iterable (i.e. an iterator can be made from them) so when you say for k in d.keys() Python will create the iterator for you. Therefore your two examples will behave the same.

keys()的返回类型变化的意义在于Python 3的视图对象是动态的.也就是说,如果我们说 ks = d.keys() 然后添加到 d 那么 ks 将反映这一点.在 Python 2 中,keys() 返回字典中当前所有键的列表.比较:

The significance in the change of the return type for keys() is that the Python 3 view object is dynamic. i.e. if we say ks = d.keys() and later add to d then ks will reflect this. In Python 2, keys() returns a list of all the keys currently in the dict. Compare:

Python 3

>>> d = { "first" : 1, "second" : 2 }
>>> ks = d.keys()
>>> ks
dict_keys(['second', 'first'])
>>> d["third"] = 3
>>> ks
dict_keys(['second', 'third', 'first'])

Python 2.x

>>> d = { "first" : 1, "second" : 2 }
>>> ks = d.keys()
>>> ks
['second', 'first']
>>> d["third"] = 3
>>> ks
['second', 'first']

由于 Python 3 的 keys() 返回动态对象,Python 3 没有(也不需要)单独的 iterkeys 方法.

As Python 3's keys() returns the dynamic object Python 3 doesn't have (and has no need for) a separate iterkeys method.

进一步说明

在 Python 3 中,keys() 返回一个 dict_keys 对象,但如果我们在 for 循环上下文中使用它 for k在 d.keys() 然后隐式创建一个迭代器.因此 for k in d.keys()for k in iter(d.keys()) 之间的区别是迭代器的隐式与显式创建之一.

In Python 3, keys() returns a dict_keys object but if we use it in a for loop context for k in d.keys() then an iterator is implicitly created. So the difference between for k in d.keys() and for k in iter(d.keys()) is one of implicit vs. explicit creation of the iterator.

另一个区别是,虽然它们都是动态的,但请记住,如果我们创建一个显式迭代器,那么它只能使用一次,而视图可以根据需要重复使用.例如

In terms of another difference, whilst they are both dynamic, remember if we create an explicit iterator then it can only be used once whereas the view can be reused as required. e.g.

>>> ks = d.keys()
>>> 'first' in ks
True
>>> 'second' in ks
True
>>> i = iter(d.keys())
>>> 'first' in i
True
>>> 'second' in i
False             # because we've already reached the end of the iterator

另外,请注意,如果我们创建一个显式迭代器,然后修改 dict,那么迭代器就会失效:

Also, notice that if we create an explicit iterator and then modify the dict then the iterator is invalidated:

>>> i2 = iter(d.keys())
>>> d['fourth'] = 4
>>> for k in i2: print(k)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: dictionary changed size during iteration

在 Python 2 中,鉴于 keys 的现有行为,需要一个单独的方法来提供一种在不复制键列表的情况下进行迭代的方法,同时仍保持向后兼容性.因此 iterkeys()

In Python 2, given the existing behaviour of keys a separate method was needed to provide a way to iterate without copying the list of keys whilst still maintaining backwards compatibility. Hence iterkeys()

相关文章