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

Python 相當(dāng)于 zip 的字典

Python equivalent of zip for dictionaries(Python 相當(dāng)于 zip 的字典)
本文介紹了Python 相當(dāng)于 zip 的字典的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

如果我有這兩個列表:

la = [1, 2, 3]
lb = [4, 5, 6]

我可以如下迭代它們:

for i in range(min(len(la), len(lb))):
    print la[i], lb[i]

或者更多的蟒蛇

for a, b in zip(la, lb):
    print a, b

<小時>

如果我有兩本字典怎么辦?


What if I have two dictionaries?

da = {'a': 1, 'b': 2, 'c': 3}
db = {'a': 4, 'b': 5, 'c': 6}

同樣,我可以手動迭代:

Again, I can iterate manually:

for key in set(da.keys()) & set(db.keys()):
    print key, da[key], db[key]

是否有一些內(nèi)置方法可以讓我進(jìn)行如下迭代?

Is there some builtin method that allows me to iterate as follows?

for key, value_a, value_b in common_entries(da, db):
    print key, value_a, value_b 

推薦答案

沒有內(nèi)置函數(shù)或方法可以做到這一點(diǎn).但是,您可以輕松定義自己的.

There is no built-in function or method that can do this. However, you could easily define your own.

def common_entries(*dcts):
    if not dcts:
        return
    for i in set(dcts[0]).intersection(*dcts[1:]):
        yield (i,) + tuple(d[i] for d in dcts)

這建立在手動方法"的基礎(chǔ)上.您提供,但像 zip 一樣,可用于任意數(shù)量的字典.

This builds on the "manual method" you provide, but, like zip, can be used for any number of dictionaries.

>>> da = {'a': 1, 'b': 2, 'c': 3}
>>> db = {'a': 4, 'b': 5, 'c': 6}
>>> list(common_entries(da, db))
[('c', 3, 6), ('b', 2, 5), ('a', 1, 4)]

當(dāng)只提供一個字典作為參數(shù)時,它本質(zhì)上返回 dct.items().

When only one dictionary is provided as an argument, it essentially returns dct.items().

>>> list(common_entries(da))
[('c', 3), ('b', 2), ('a', 1)]

沒有字典,它返回一個空的生成器(就像 zip())

With no dictionaries, it returns an empty generator (just like zip())

>>> list(common_entries())
[]

這篇關(guān)于Python 相當(dāng)于 zip 的字典的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 午夜a区 | 欧美日韩久久精品 | 日韩在线不卡 | 一区二区三区四区免费视频 | 精品一区在线免费观看 | 亚洲一区二区在线播放 | 欧美一区二区在线免费观看 | 久久一及片| 男女污网站 | av黄色片在线观看 | 欧美一区二区三区在线观看 | 午夜久久 | 亚洲第一在线 | 在线观看黄色电影 | 成人影院一区二区三区 | 欧美在线国产精品 | 亚洲三区视频 | 精品久久久久久亚洲精品 | 亚洲综合成人网 | 国产999精品久久久久久 | 日本精品一区二区 | 亚洲国产视频一区 | 国产成人一区二区三区精 | 国产精品一区二区av | 综合欧美亚洲 | 两性午夜视频 | 精品成人av| 久久精品无码一区二区三区 | 欧美精品91| 激情婷婷 | 中文字幕一区在线观看视频 | 精品欧美激情精品一区 | 91在线精品一区二区 | 欧美中文字幕一区二区 | 日本不卡免费新一二三区 | 免费福利视频一区二区三区 | 久久国产视频一区 | 老子午夜影院 | 91伊人| 在线亚洲免费 | 成人精品免费 |