[英]Concatenating Unique Values from List of Dictionaries with sames Keys in Python
I am working with a list of dictionaries and am trying to combine them into one with the following conditions: 我正在处理字典列表,并尝试将它们与以下条件组合为一个字典:
So really what I am trying to do is create a set for a given key across multiple dictionaries, and create a comma seperated string of that set for the value in a new dictionary. 因此,实际上,我要尝试的是为多个字典中的给定键创建一个集合,并为新字典中的值创建一个用逗号分隔的字符串。 To help visualize, given:
为了帮助可视化,给出:
data = [ {"key1": "value1", "key2": "value2", "key3": "value3"},
{"key1": "value4", "key2": "value5", "key3": "value3"},
{"key1": "value1", "key2": "value8", "key3": "value3"} ]
I want to make a new dictionary out of data that would like as follows: 我想根据以下数据制作一个新的字典:
myDict = {"key1": "value1, value4", "key2": "value2, value5, value8", "key3": "value3"}
Any ideas on how to accomplish this? 关于如何做到这一点的任何想法?
collections.defaultdict
is your friend. 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)
That gives you a dict in the form {"key1": ["value1", "value4"]}
- if you want actual comma-separated strings for the values then you can join them: 这样会以
{"key1": ["value1", "value4"]}
的形式给您一个字典-如果您想要用逗号分隔的字符串作为值,则可以将它们连接起来:
my_dict = {}
for key, value in temp_dict.items():
my_dict[key] = ", ".join(value)
I presumed two things here - that order of the values was not important to you, and that you did not mean to have two of the same keys in each dictionary (which I've replaced with "key3"
): 我在这里假定了两件事-值的顺序对您而言并不重要,并且您并不是要在每个字典中都拥有两个相同的键(我已将其替换为
"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'}
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'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.