Merge two 2D dictionaries python

LeoCella

I have two 2D python dictionaries, and I want to get a single 2D dictionary where the keys are the union of the first two dictionaries' keys and the values a concatenation of the 1d dictionary. In case of duplicate keys in the 1D dictionaries, I would like to have the sum of their values.

So, supposing that I've :

dict1 = { 1: {2: 0.1, 3: 0.3, 4: 0.4} , 
          2 :{1: 0.2, 3: 0.3, 4: 0.5} }
dict2 = { 2: {1: 0.1, 3: 0.8, 5: 0.4} , 
          3 :{1: 0.2, 2: 0.8, 4: 0.5} }

what I expect to have is:

merged_dict = { 1: {2: 0.1, 3: 0.3, 4: 0.4} , 
                2 :{1: 0.3, 3: 1.1, 4: 0.5 , 5: 0.4} , 
                3: {1: 0.2, 2: 0.8, 4: 0.5} }

I'm currently using python 3.2.3

Julien Spronck

Nothing fancy here but you can always do something like:

merged_dict = dict1.copy()

for key, val in dict2.iteritems():
    if key not in dict1:
        merged_dict[key] = val
    else:
        for key2, val2 in val.iteritems():
            if key2 not in dict1[key]:
                merged_dict[key][key2] = val2
            else:
                merged_dict[key][key2] += val2

(sorry that's python 2.7)

Also, if you want to use defaultdict:

from collections import defaultdict
merged_dict = defaultdict(dict)

for key, val in dict1.iteritems():
    merged_dict[key] = defaultdict(int, val)
for key, val in dict2.iteritems():
    if key not in merged_dict:
        merged_dict[key] = defaultdict(int, val)
    else:
        for key2, val2 in val.iteritems():
            merged_dict[key][key2] += val2

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

To merge two dictionaries of list in Python

From Dev

Merge two dictionaries in python with lists as values

From Dev

Merge two dictionaries in python with lists as values

From Dev

merge two dictionaries by key

From Dev

merge two dictionaries by key

From Dev

Merge two dictionaries

From Dev

In Python merge two dictionaries so that their keys are added/subtracted

From Dev

Merge two dictionaries and keep the values for duplicate keys in Python

From Dev

Python - merge two list of dictionaries adding values of repeated keys

From Dev

Merge two list into a list of dictionaries with multiple keys in python

From Dev

Merge three dictionaries in Python

From Dev

Generic function to merge two dictionaries

From Dev

How to merge two dictionaries in javascript

From Java

How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?

From Dev

Merge two dictionaries with nested dictionaries into new one, summing same keys and keeping unaltered ones in Python

From Dev

Sorting 2D Dictionaries In Python

From Java

Intersecting two dictionaries in Python

From Dev

compare two dictionaries in python

From Dev

Merge python dictionaries with common key

From Dev

How to merge two dictionaries with same key names

From Dev

Merge Two Dictionaries that Share Same Key:Value

From Dev

Merge two (or more) identical dictionaries having just one value in common - in Python

From Dev

Comparing two lists of dictionaries in Python

From Dev

python merge list of dictionaries based on key

From Dev

Merge dictionaries from multiple lists in Python

From Dev

Python - merge dictionaries adding values of repeated keys

From Dev

merge x python dictionaries into 1 with aggregated values

From Dev

Merge two dataframes with python

From Dev

How can I merge two nested dictionaries together?

Related Related

  1. 1

    To merge two dictionaries of list in Python

  2. 2

    Merge two dictionaries in python with lists as values

  3. 3

    Merge two dictionaries in python with lists as values

  4. 4

    merge two dictionaries by key

  5. 5

    merge two dictionaries by key

  6. 6

    Merge two dictionaries

  7. 7

    In Python merge two dictionaries so that their keys are added/subtracted

  8. 8

    Merge two dictionaries and keep the values for duplicate keys in Python

  9. 9

    Python - merge two list of dictionaries adding values of repeated keys

  10. 10

    Merge two list into a list of dictionaries with multiple keys in python

  11. 11

    Merge three dictionaries in Python

  12. 12

    Generic function to merge two dictionaries

  13. 13

    How to merge two dictionaries in javascript

  14. 14

    How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?

  15. 15

    Merge two dictionaries with nested dictionaries into new one, summing same keys and keeping unaltered ones in Python

  16. 16

    Sorting 2D Dictionaries In Python

  17. 17

    Intersecting two dictionaries in Python

  18. 18

    compare two dictionaries in python

  19. 19

    Merge python dictionaries with common key

  20. 20

    How to merge two dictionaries with same key names

  21. 21

    Merge Two Dictionaries that Share Same Key:Value

  22. 22

    Merge two (or more) identical dictionaries having just one value in common - in Python

  23. 23

    Comparing two lists of dictionaries in Python

  24. 24

    python merge list of dictionaries based on key

  25. 25

    Merge dictionaries from multiple lists in Python

  26. 26

    Python - merge dictionaries adding values of repeated keys

  27. 27

    merge x python dictionaries into 1 with aggregated values

  28. 28

    Merge two dataframes with python

  29. 29

    How can I merge two nested dictionaries together?

HotTag

Archive