将键/值从一个dict复制到另一个dict的Pythonic方法
我正在寻找一种简洁的方法,获取两个具有公共键/值的dict,并将一个键和值复制到其中一个dict中。例如:将键/值从一个dict复制到另一个dict的Pythonic方法,python,dictionary,Python,Dictionary,我正在寻找一种简洁的方法,获取两个具有公共键/值的dict,并将一个键和值复制到其中一个dict中。例如: 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 = [
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
。如果可能的话,我正在寻找python方法。您可以使用dict()
复制一个字典并传入额外的键。您需要首先创建从uid
到orderid
的映射:
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
中
演示:
您可以使用dict()
复制一个字典并传入额外的键。您需要首先创建从uid
到orderid
的映射:
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
中
演示:
回答得很好@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'}]
`
回答得很好@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'}]
`
你的意思可能是
dict2['orderid']
?如果它们没有排序,会发生什么?@C.B.:啊,误解了。是的,在这种情况下,这是两个独立的数据源,我需要为它们创建一个输出。顺序可以是任何东西。你的意思可能是dict2['orderid']
?如果它们没有排序,会发生什么?@C.B.:啊,误解了。是的,在这种情况下,这是两个独立的数据源,我需要为它们创建一个输出。排序完全可以是任何东西。列表项是否总是按照相应的顺序排列?d1[i][uid”]==d2[i][uid”]
是否对所有i
均为真?重复:列表项是否始终按相应顺序排列?所有i
的d1[i][uid]==d2[i][uid]
是否为真?重复: