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

python dict理解總是“最后贏"嗎?如果有重復的

Is a python dict comprehension always quot;last winsquot; if there are duplicate keys(python dict理解總是“最后贏嗎?如果有重復的鍵)
本文介紹了python dict理解總是“最后贏"嗎?如果有重復的鍵的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如果我創建一個帶有 dict 理解的 python 字典,但有重復的鍵,我是否保證最后一項將是最終字典中的那個?我看不清楚 https://www.python.org/dev/peps/pep-0274/?

If I create a python dictionary with a dict comprehension, but there are duplicate keys, am I guaranteed that the last item will be the one that ends up in the final dictionary? It's not clear to me from looking at https://www.python.org/dev/peps/pep-0274/?

new_dict = {k:v for k,v in [(1,100),(2,200),(3,300),(1,111)]}
new_dict[1] #is this guaranteed to be 111, rather than 100?

推薦答案

鍵的最后一個值獲勝.我能找到的最好的文檔在 Python 3 語言參考部分6.2.7:

The last value for a key wins. The best documentation I can find for this is in the Python 3 language reference, section 6.2.7:

與列表和集合推導相比,字典推導需要兩個用冒號隔開的表達式,后跟通常的for"和if"子句.運行推導時,生成的鍵和值元素按照它們產生的順序插入到新字典中.

A dict comprehension, in contrast to list and set comprehensions, needs two expressions separated with a colon followed by the usual "for" and "if" clauses. When the comprehension is run, the resulting key and value elements are inserted in the new dictionary in the order they are produced.

該文檔還明確指出,最后一項對于逗號分隔的鍵值對({1: 1, 1: 2})和字典解包({**{1:1},**{1:2}}):

That documentation also explicitly states that the last item wins for comma-separated key-value pairs ({1: 1, 1: 2}) and for dictionary unpacking ({**{1: 1}, **{1: 2}}):

如果給出了以逗號分隔的鍵/數據對序列,...您可以在鍵/數據列表中多次指定同一個鍵,并且該鍵的最終字典值將是最后一個給出的值.

If a comma-separated sequence of key/datum pairs is given, ... you can specify the same key multiple times in the key/datum list, and the final dictionary’s value for that key will be the last one given.

雙星號** 表示字典解包.它的操作數必須是一個映射.每個映射項都添加到新字典中.以后的值替換已由較早的鍵/數據對和較早的字典解包設置的值.

A double asterisk ** denotes dictionary unpacking. Its operand must be a mapping. Each mapping item is added to the new dictionary. Later values replace values already set by earlier key/datum pairs and earlier dictionary unpackings.

請注意,wim 指出,如果有相同但不同的鍵,則鍵的第一個版本獲勝:

Note that as wim points out, the first version of a key wins if there are equal but distinct keys:

>>> {k: v for k, v in [(1, 1), (1.0, 2.0)]}
{1: 2.0}

這里,最終的 dict 的鍵來自 (1, 1),但值來自 (1.0, 2.0).

Here, the final dict has the key from (1, 1), but the value from (1.0, 2.0).

這篇關于python dict理解總是“最后贏"嗎?如果有重復的鍵的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
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(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产精品高清在线 | 成人免费在线视频 | 国产精品国产馆在线真实露脸 | 四虎影 | 婷婷色婷婷 | 欧美午夜精品 | 久久中文字幕电影 | 欧美成人a∨高清免费观看 色999日韩 | 久久国产麻豆 | 久久高潮 | 免费视频成人国产精品网站 | 久久久久国产精品人 | 免费成人在线网站 | 毛片在线免费 | 成人免费视频网站在线看 | 在线第一页 | 精品久久久久一区二区国产 | 精品久草 | 欧美久久精品 | 亚洲精品在线视频 | 欧美一区二区三区 | 成人免费视屏 | 性色av一区二区三区 | 欧美日韩一二区 | 国产农村妇女毛片精品久久麻豆 | 自拍偷拍亚洲视频 | 国产日韩久久 | 国产成人综合一区二区三区 | 国产精品久久久久久久 | 欧美成人精品一区 | 中文日韩在线 | 国产成人啪免费观看软件 | 国产情侣在线看 | 久操福利| 99热热99 | 在线午夜 | 夜夜爽99久久国产综合精品女不卡 | 蜜桃综合在线 | 黄色在线免费观看视频网站 | 一区二区三区免费看 | 国产在线视频一区二区董小宛性色 |