일반적인 키/값을 가진 두 개의 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
키와 일치하는 데이터 포인트에 대한 값을 복사하기 위해 사용하고자하는 키이다. orderid
가 d1
로 끌어
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'}
]
: 결국 내가 좋아하는 뭔가를 얻을 것입니다. 나는 가능하다면 파이썬 방법을 찾고 있습니다.
이됩니다 항목을 항상 해당 순서로 나열 하시겠습니까? 'd1 [i] [ "uid"] == d2 [i] [ "uid"]'모든'i'에 대해 true입니까? – Messa
duplicate : http://stackoverflow.com/questions/3421906/how-to-merge-lists-of-dictionaries – zhangxaochen