2014-02-13 2 views
1

일반적인 키/값을 가진 두 개의 dict을 취하고 dicts 중 하나에 키와 값을 복사하는 간결한 방법을 찾고 있습니다. 예 :하나의 dict에서 다른 dict로 키/값을 복사하는 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 키와 일치하는 데이터 포인트에 대한 값을 복사하기 위해 사용하고자하는 키이다. orderidd1로 끌어

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

: 결국 내가 좋아하는 뭔가를 얻을 것입니다. 나는 가능하다면 파이썬 방법을 찾고 있습니다.

+0

이됩니다 항목을 항상 해당 순서로 나열 하시겠습니까? 'd1 [i] [ "uid"] == d2 [i] [ "uid"]'모든'i'에 대해 true입니까? – Messa

+0

duplicate : 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 값이 고유하고 에있는 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'}] 

` 
관련 문제