我正在尋找一種簡單的方法,即採用兩個具有共同鍵/值的字典,並將密鑰和值複製到其中一個字典中。例如:從一個字典複製密鑰/值到另一個的Python方式
d1 = [{'name': 'john', 'uid': 'ax01', 'phone': '555-555-5555'},
{'name': 'jane', 'uid': 'ax02', 'phone': '555-555-5555'},
{'name': 'jimmy', 'uid': 'ax03', 'phone': '555-555-5555'}]
d2 = [{'uid': 'ax01', 'orderid': '9999', 'note': 'testing this'},
{'uid': 'ax02', 'orderid': '6666', 'note': 'testing this'},
{'uid': 'ax03', 'orderid': '7777', 'note': 'testing this'}]
這裏uid
是,我要用來然後複製orderid
鍵和該匹配的數據點的值的關鍵。最後,我會得到這樣的:
output = [
{'name': 'john', 'uid': 'ax01', 'phone': '555-555-5555', 'orderid': '9999'},
{'name': 'jane', 'uid': 'ax02', 'phone': '555-555-5555', 'orderid': '6666'},
{'name': 'jimmy', 'uid': 'ax03', 'phone': '555-555-5555', 'orderid': '7777'}
]
凡orderid
被拉入d1
。如果可能的話,我正在尋找pythonic途徑。
是列出項目總是按相應的順序?是'd1 [i] [「uid」] == d2 [i] [「uid」]對於所有'i'都是真的嗎? – Messa
重複:http://stackoverflow.com/questions/3421906/how-to-merge-lists-of-dictionaries – zhangxaochen