2014-02-13 52 views
1

我正在尋找一種簡單的方法,即採用兩個具有共同鍵/值的字典,並將密鑰和值複製到其中一個字典中。例如:從一個字典複製密鑰/值到另一個的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途徑。

+0

是列出項目總是按相應的順序?是'd1 [i] [「uid」] == d2 [i] [「uid」]對於所有'i'都是真的嗎? – Messa

+0

重複:http://stackoverflow.com/questions/3421906/how-to-merge-lists-of-dictionaries – zhangxaochen

回答

5

您可以使用dict()複製一個字典並傳入額外的密鑰。你需要從uidorderid第一,雖然創建一個映射:

uid_to_orderid = {d['uid']: d['orderid'] for d in d2} 
output = [dict(d, orderid=uid_to_orderid[d['uid']]) for d in d1] 

這是假設你要離開的字典中d1不變否則。其他假設是uid值是唯一的,d1中的所有uid值都存在於d2中。

演示:

>>> 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_to_orderid = {d['uid']: d['orderid'] for d in d2} 
>>> [dict(d, orderid=uid_to_orderid[d['uid']]) for d in d1] 
[{'orderid': '9999', 'phone': '555-555-5555', 'name': 'john', 'uid': 'ax01'}, {'orderid': '6666', 'phone': '555-555-5555', 'name': 'jane', 'uid': 'ax02'}, {'orderid': '7777', 'phone': '555-555-5555', 'name': 'jimmy', 'uid': 'ax03'}] 
+0

你可能是指'dict2 ['orderid']'?如果他們沒有訂購會發生什麼? –

+0

@ C.B.:啊,誤解了。 –

+0

是的,在這種情況下,這些是兩個獨立的數據源,然後我需要爲其創建輸出。排序可以完全是任何東西。 – BryanGrimes

0

大答案@Martijn皮特斯

我想不出來防止丟失數據以外的東西粗野這樣一個聰明的辦法:

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'}, 
    {'name': 'jack', 'uid': 'ax04', 'phone': '555-555-5555'}, 
    {'name': 'joan', 'uid': 'ax05', 'phone': '555-555-5555'}] 
d2 = [{'uid': 'ax01', 'orderid': '9999', 'note': 'testing this'}, 
     {'uid': 'ax02', 'orderid': '6666', 'note': 'testing this'}, 
     {'uid': 'ax03', 'orderid': '6666', 'note': 'testing this'}, 
     {'uid': 'ax05', 'orderid-not-here': '7777', 'note': 'testing this'}] 

def get_orderid(search): 
    match = [x for x in d2 if x['uid']==search] 
    if len(match) == 0: 
     return 'None' 
    else: 
     return match[0].get('orderid','None') 


[dict(d, orderid=get_orderid(d['uid'])) for d in d1] 


[ {'name': 'john', 'orderid': '9999', 'phone': '555-555-5555', 'uid': 'ax01'}, 
    {'name': 'jane', 'orderid': '6666', 'phone': '555-555-5555', 'uid': 'ax02'}, 
    {'name': 'jimmy', 'orderid': '6666', 'phone': '555-555-5555', 'uid': 'ax03'}, 
    {'name': 'jack', 'orderid': 'None', 'phone': '555-555-5555', 'uid': 'ax04'}, 
    {'name': 'joan', 'orderid': 'None', 'phone': '555-555-5555', 'uid': 'ax05'}] 

` 
相關問題