简体   繁体   English

将字典列表中的唯一值与Python中的相同键串联在一起

[英]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: 我正在处理字典列表,并尝试将它们与以下条件组合为一个字典:

  1. The dictionaries in the list all have the same keys, and the new dictionary will as well 列表中的字典都具有相同的键,新字典也会
  2. If the values for a given key across all dictionaries is the same, then that value will appear once in the value for the new dictionary 如果所有字典中给定键的值都相同,则该值将在新字典的值中出现一次
  3. If the values for a given key across as dictionaries are unique, then the value for that key in the new dictionary will be a comma seperated string of all the values 如果字典中给定键的值是唯一的,则新字典中该键的值将是所有值的逗号分隔字符串

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM