[英]Combine dictionaries based on key value
How do I combine the rows of dictionaries having same keys. 如何合并具有相同键的字典行。 For instance If I have
例如,如果我有
my_dict_list = [{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'jitu': ['gold']},
{'jitu': ['wins']},
{'atanu': ['good', 'glory']},
{'atanu': ['top', 'winner','good']}]
My objective is to get 我的目标是
my_new_dict_list = [{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'jitu': ['gold','wins']},
{'atanu': ['good', 'glory','top', 'winner','good']}]
How do I do that in Python? 如何在Python中做到这一点?
EDIT: The dictionaries in the final list must contain repeated values if present in starting list. 编辑:如果出现在起始列表中,则最终列表中的词典必须包含重复值。
You could loop over the dicts in your list and either insert or append the key-value pairs to a new dict: 您可以遍历列表中的字典,然后将键值对插入或追加到新字典中:
my_dict_list = [{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'jitu': ['gold']},
{'jitu': ['wins']},
{'atanu': ['good', 'glory']},
{'atanu': ['top', 'winner']}]
new_d = {}
for d in my_dict_list:
for k, v in d.items():
if k in new_d:
new_d[k] += v
else:
new_d[k] = v
Then you need to make a list from the result: 然后,您需要根据结果列出一个列表:
l = [{k: v} for k, v in new_d.items()]
# [{'atanu': ['good', 'glory', 'top', 'winner']}, {'gagan': ['good', 'luck']}, {'prakash': ['confident']}, {'jitu': ['gold', 'wins']}]
You need to be aware that the order of the items in the list may be changed by that. 您需要注意,列表中项目的顺序可能会因此改变。
Here's a working example: 这是一个工作示例:
from itertools import groupby
my_dict_list = [
{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'jitu': ['gold']},
{'jitu': ['wins']},
{'atanu': ['good', 'glory']},
{'atanu': ['top', 'winner']}
]
my_new_dict_list = []
for k, g in groupby(my_dict_list, key=lambda x: sorted(x.keys())):
ds = list(g)
d = {}
for k in ds[0].iterkeys():
d[k] = sum([d[k] for d in ds], [])
my_new_dict_list .append(d)
print my_new_dict_list
my_dict_list = [{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'jitu': ['gold']},
{'jitu': ['wins']},
{'atanu': ['good', 'glory']},
{'atanu': ['top', 'winner','good']}]
my_new_dict_list = []
tmp_dict = {}
order = []
for d in my_dict_list:
for k, v in d.iteritems():
if not k in order: order.append(k)
tmp_dict.setdefault(k, []).extend(v)
my_new_dict_list = [ {x: tmp_dict[x] } for x in order ]
Output: 输出:
[{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'jitu': ['gold', 'wins']},
{'atanu': ['good', 'glory', 'top', 'winner', 'good']}]
a minimalist approach using defaultdict
: 使用
defaultdict
的极简方法:
from collections import defaultdict
my_dict_list = [{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'jitu': ['gold']},
{'jitu': ['wins']},
{'atanu': ['good', 'glory']},
{'atanu': ['top', 'winner','good']}]
merged_dict = defaultdict(list)
for d in my_dict_list:
for key, value in d.items():
merged_dict[key].extend(value)
result = [{key:value} for key, value in merged_dict.items()]
print(result)
Output 产量
[{'prakash': ['confident']},
{'gagan': ['good', 'luck']},
{'atanu': ['good', 'glory', 'top', 'winner', 'good']},
{'jitu': ['gold', 'wins']}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.