Join multiple dicts to create new list with value as list of values of original dict

前端 未结 3 1278
小鲜肉
小鲜肉 2021-01-29 06:46

I\'m on Python 2.7 and have looked at several solutions here which works if you know how many dictionaries you are merging, but I could have anything between 2 to 5.

I

相关标签:
3条回答
  • 2021-01-29 07:08

    Use a defaultdict:

    In [18]: def gen_dictionaries():
        ...:     yield {'key1': 1, 'key2': 2, 'key3': 3}
        ...:     yield {'key1': 4, 'key2': 5, 'key3': 6}
        ...:
    
    In [19]: from collections import defaultdict
    
    In [20]: final = defaultdict(list)
    
    In [21]: for d in gen_dictionaries():
        ...:     for k, v in d.iteritems():
        ...:         final[k].append(v)
        ...:
    
    In [22]: final
    Out[22]: defaultdict(list, {'key1': [1, 4], 'key2': [2, 5], 'key3': [3, 6]})
    
    0 讨论(0)
  • 2021-01-29 07:11
    dict1 = {'m': 2, 'n':4, 'o':7, 'p':90}
    dict2 = {'m': 1, 'n': 3}
    
    
    dict3 = {}
    
    for key,value in dict1.iteritems():
        if key not in dict3:
            dict3[key] = list()
        dict3[key].append(value)
    
    
    for key,value in dict2.iteritems():
        if key not in dict3:
            dict3[key] = list()
        dict3[key].append(value)
    
    
    print(dict3)
    

    The output looks like this :

    {'p': [90], 'm': [2, 1], 'o': [7], 'n': [4, 3]}

    0 讨论(0)
  • 2021-01-29 07:30

    One way to achieve this in a generic way is via using set to find the union of keys of both the dicts and then use a dictionary comprehension to get the desired dict as:

    >>> dict_list = [d1, d2]  # list of all the dictionaries which are to be joined
    
     # set of the keys present in all the dicts; 
    >>> common_keys = set(dict_list[0]).union(*dict_list[1:])
    
    >>> {k: [d[k] for d in dict_list if k in d] for k in common_keys}
    {'key3': [3, 6], 'key2': [2, 5], 'key1': [1, 4]}
    

    where d1 and d2 are:

    d1 = {'key1': 1,
          'key2': 2,
          'key3': 3}
    
    d2 = {'key1': 4,
          'key2': 5,
          'key3': 6}
    

    Explanation: Here, dict_list is the list of all the dict objects you want to combine. Then I am creating the common_keys set of the keys in all the dict object. Finally I am creating a new dictionary via using dictionary comprehension (with nested list comprehension with filter).


    Based on comment from OP, since all the dicts hold the same keys, we can skip the usage of set. Hence the code could be written as:

    >>> dict_list = [d1, d2]  
    
    >>> {k: [d[k] for d in dict_list] for k in dict_list[0]}
    {'key3': [3, 6], 'key2': [2, 5], 'key1': [1, 4]}
    
    0 讨论(0)
提交回复
热议问题