我正在处理一个词典列表,并尝试将它们组合成一个具有以下条件的词典:
所以我真正要做的是为多个字典中的给定键创建一个集合,并为新字典中的值创建该集合的逗号分隔字符串。为了帮助可视化,给出:
data = [ {"key1": "value1", "key2": "value2", "key3": "value3"},
{"key1": "value4", "key2": "value5", "key3": "value3"},
{"key1": "value1", "key2": "value8", "key3": "value3"} ]
我想用以下数据创建一个新词典:
myDict = {"key1": "value1, value4", "key2": "value2, value5, value8", "key3": "value3"}
关于如何实现这一目标的任何想法?
答案 0 :(得分:5)
collections.defaultdict
是你的朋友。
from collections import defaultdict
temp_dict = defaultdict(set)
for item in data:
for key, value in item.items():
temp_dict[key].add(value)
它会以{"key1": ["value1", "value4"]}
的形式为您提供一个字典 - 如果您想要值的实际以逗号分隔的字符串,那么您可以加入它们:
my_dict = {}
for key, value in temp_dict.items():
my_dict[key] = ", ".join(value)
答案 1 :(得分:3)
我在这里假定了两件事 - 值的顺序对你来说并不重要,并且你并不是说在每个字典中都有两个相同的键(我用"key3"
代替):
>>> data = [ {"key1": "value1", "key2": "value2", "key3": "value3"},
... {"key1": "value4", "key2": "value5", "key3": "value3"},
... {"key1": "value1", "key2": "value8", "key3": "value3"} ]
>>>
>>> keylist = data[0].keys()
>>> mydata = dict((k,', '.join(set(map(lambda d: d[k], data)))) for k in keylist)
>>> mydata
{'key3': 'value3', 'key2': 'value5, value2, value8', 'key1': 'value4, value1'}
答案 2 :(得分:1)
In [3]: from itertools import chain
In [12]: dict([ (key, ",".join(set([elem[key] for elem in data]))) for key in set(list(chain(*[d.keys() for d in data])))])
Out[12]: {'key1': 'value4,value1', 'key2': 'value5,value2,value8', 'key3': 'value3'}