dict_1 = [
{'INCIDENT_ID': 'SD000001372596', 'FIRST_CALL': 't'}
{'INCIDENT_ID': 'SD000001372594', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372598', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372599', 'FIRST_CALL': 't'}
{'INCIDENT_ID': 'SD000001372602', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372601', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372605', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372606', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372607', 'FIRST_CALL': 'f'}
]
dict_2 = [
{'INCIDENT_ID': 'SD000001372605', 'DATE': '08-10-2016 00:54:13'}
{'INCIDENT_ID': 'SD000001372606', 'DATE': '08-10-2016 00:57:20'}
{'INCIDENT_ID': 'SD000001372607', 'DATE': '08-10-2016 01:00:25'}
{'INCIDENT_ID': 'SD000001372598', 'DATE': '11-10-2016 10:57:34'}
{'INCIDENT_ID': 'SD000001372602', 'DATE': '08-10-2016 10:44:34'}
{'INCIDENT_ID': 'SD000001372601', 'DATE': '21-10-2016 22:30:49'}
{'INCIDENT_ID': 'SD000001372594', 'DATE': '18-10-2016 14:53:34'}
]
我有两个不同长度的词典列表,我想根据“INCIDENT_ID”将(“dict_2”日期)添加到“dict_1”
答案 0 :(得分:1)
您可以使用defaultdict
模块中的collections
:
from collections import defaultdict
dct = defaultdict(dict)
for i in zip(dict_1, dict_2):
for j in i:
dct[j["INCIDENT_ID"]].update(j)
solution_dict = list(dct.values())
所以,现在solution_dict
将为您提供组合对的词典列表:
[ {'INCIDENT_ID' : 'SD000001372607', 'DATE': '08-10-2016 01:00:25'}
{'INCIDENT_ID': 'SD000001372601', 'DATE': '21-10-2016 22:30:49', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372606', 'DATE': '08-10-2016 00:57:20'}
{'INCIDENT_ID': 'SD000001372598', 'DATE': '11-10-2016 10:57:34', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372596', 'FIRST_CALL': 't'}
{'INCIDENT_ID': 'SD000001372594', 'DATE': '18-10-2016 14:53:34', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372605', 'DATE': '08-10-2016 00:54:13', 'FIRST_CALL': 'f'}
{'INCIDENT_ID': 'SD000001372599', 'FIRST_CALL': 't'}
{'INCIDENT_ID': 'SD000001372602', 'DATE': '08-10-2016 10:44:34', 'FIRST_CALL': 'f'} ]