久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

Iterating over dictionary items(), values(), keys() in Python 3(在 Python 3 中迭代字典 items()、values()、keys())
本文介紹了在 Python 3 中迭代字典 items()、values()、keys()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如果我理解正確,在 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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 欧美一级久久久猛烈a大片 日韩av免费在线观看 | 中文在线视频观看 | 免费成人高清 | 亚洲精品在线免费观看视频 | 精品久久香蕉国产线看观看亚洲 | 午夜久久久 | 欧美日韩专区 | www.久草| 国产激情三区 | 视频一区二区三区中文字幕 | 黄色成人在线观看 | 久久亚洲国产精品日日av夜夜 | 欧美情趣视频 | 国产伦精品一区二区三区四区视频 | 欧美一级久久 | 久久久精品 | 日韩一三区 | 亚洲一区二区三区在线播放 | 亚洲精品一区二区冲田杏梨 | 免费成人高清在线视频 | 精品国产免费一区二区三区演员表 | 成人午夜电影在线观看 | 成人精品一区二区三区 | a在线观看 | 日韩欧美第一页 | 色综合天天天天做夜夜夜夜做 | 亚洲精品99 | 亚洲成人精选 | 久久久久久久久久久久一区二区 | 久久最新精品视频 | 久久国产精品免费一区二区三区 | 国产精品久久久久久久午夜片 | 一区二区三区免费网站 | 欧产日产国产精品视频 | 国产精品国产成人国产三级 | 涩涩视频网站在线观看 | 日韩欧美综合 | 性一区| 亚洲视频一区二区三区 | 可以看黄的视频 | 在线观看av网站 |