Pythonic方法将键/值从一个字典复制到另一个字典

时间:2014-02-13 16:41:11

标签: python dictionary

我正在寻找一种简洁的方法来获取具有共同键/值的两个dicts,并将键和值复制到其中一个词组中。例如:

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方式。

2 个答案:

答案 0 :(得分: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值是唯一的,uid中的所有d1值都存在于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'}]

答案 1 :(得分:0)

很棒的答案@Martijn Pieters

我想不出一种聪明的方法可以防止丢失数据,而不像其他类似的东西:

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'}]

`