How to merge multiple dicts with same key?

前端 未结 14 2390
别跟我提以往
别跟我提以往 2020-11-22 13:12

I have multiple dicts/key-value pairs like this:

d1 = {key1: x1, key2: y1}  
d2 = {key1: x2, key2: y2}  

I want the result to be a new di

相关标签:
14条回答
  • 2020-11-22 13:35

    Here's a general solution that will handle an arbitrary amount of dictionaries, with cases when keys are in only some of the dictionaries:

    from collections import defaultdict
    
    d1 = {1: 2, 3: 4}
    d2 = {1: 6, 3: 7}
    
    dd = defaultdict(list)
    
    for d in (d1, d2): # you can list as many input dicts as you want here
        for key, value in d.items():
            dd[key].append(value)
    
    print(dd)
    

    Shows:

    defaultdict(<type 'list'>, {1: [2, 6], 3: [4, 7]})
    

    Also, to get your .attrib, just change append(value) to append(value.attrib)

    0 讨论(0)
  • 2020-11-22 13:35

    A compact possibility

    d1={'a':1,'b':2}
    d2={'c':3,'d':4}
    context={**d1, **d2}
    context
    {'b': 2, 'c': 3, 'd': 4, 'a': 1}
    
    0 讨论(0)
  • 2020-11-22 13:36
    def merge(d1, d2, merge):
        result = dict(d1)
        for k,v in d2.iteritems():
            if k in result:
                result[k] = merge(result[k], v)
            else:
                result[k] = v
        return result
    
    d1 = {'a': 1, 'b': 2}
    d2 = {'a': 1, 'b': 3, 'c': 2}
    print merge(d1, d2, lambda x, y:(x,y))
    
    {'a': (1, 1), 'c': 2, 'b': (2, 3)}
    
    0 讨论(0)
  • 2020-11-22 13:39

    This library helped me, I had a dict list of nested keys with the same name but with different values, every other solution kept overriding those nested keys.

    https://pypi.org/project/deepmerge/

    from deepmerge import always_merger
    
    def process_parms(args):
        temp_list = []
        for x in args:
            with open(x, 'r') as stream:
                temp_list.append(yaml.safe_load(stream))
    
        return always_merger.merge(*temp_list)
    
    0 讨论(0)
  • 2020-11-22 13:40

    Assume that you have the list of ALL keys (you can get this list by iterating through all dictionaries and get their keys). Let's name it listKeys. Also:

    • listValues is the list of ALL values for a single key that you want to merge.
    • allDicts: all dictionaries that you want to merge.
    result = {}
    for k in listKeys:
        listValues = [] #we will convert it to tuple later, if you want.
        for d in allDicts:
           try:
                fileList.append(d[k]) #try to append more values to a single key
            except:
                pass
        if listValues: #if it is not empty
            result[k] = typle(listValues) #convert to tuple, add to new dictionary with key k
    
    0 讨论(0)
  • 2020-11-22 13:42

    If you only have d1 and d2,

    from collections import defaultdict
    
    d = defaultdict(list)
    for a, b in d1.items() + d2.items():
        d[a].append(b)
    
    0 讨论(0)
提交回复
热议问题