問題描述
如果我理解正確,在 Python 2 中,iter(d.keys())
與 d.iterkeys()
相同.但是現(xiàn)在,d.keys()
是一個視圖,它位于列表和迭代器之間.視圖和迭代器有什么區(qū)別?
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 中,有什么區(qū)別
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
循環(huán)中顯示出來(如果有的話)?
Also, how do these differences show up in a simple for
loop (if at all)?
推薦答案
我不確定這是否能很好地回答你的問題,但希望它能解釋一下 Python 2 和 3 之間的區(qū)別尊重.
在 Python 2 中,iter(d.keys())
和 d.iterkeys()
并不完全等效,盡管它們的行為相同.在第一個中,keys()
將返回字典的鍵列表的副本,然后 iter
將在此列表上返回一個迭代器對象,第二個是永遠不會構(gòu)建完整的鍵列表.
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 將為您創(chuàng)建迭代器.因此,您的兩個示例的行為將相同.
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的視圖對象是動態(tài)的.也就是說,如果我們說 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()
返回動態(tài)對象,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
循環(huán)上下文中使用它 for k在 d.keys()
然后隱式創(chuàng)建一個迭代器.因此 for k in d.keys()
和 for k in iter(d.keys())
之間的區(qū)別是迭代器的隱式與顯式創(chuàng)建之一.
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.
另一個區(qū)別是,雖然它們都是動態(tài)的,但請記住,如果我們創(chuàng)建一個顯式迭代器,那么它只能使用一次,而視圖可以根據(jù)需要重復使用.例如
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
另外,請注意,如果我們創(chuàng)建一個顯式迭代器,然后修改 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
的現(xiàn)有行為,需要一個單獨的方法來提供一種在不復制鍵列表的情況下進行迭代的方法,同時仍保持向后兼容性.因此 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()
這篇關(guān)于在 Python 3 中迭代字典 items()、values()、keys()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!