How to merge two dictionaries together and add their values if the keys are the same and the values are arrays?

Question:

I have two dictionaries and the keys are the same but the values are arrays, please see below:

print(helix1_coords)
‘A-85-CA’: [array([ 50.393, -20.181, 12.316], dtype=float32)], ‘A-86-CA’: [array([ 52.819, -18.757, 14.976], dtype=float32)]}…’

print(helix2_coords)
‘A-175-CA’: [array([ 31.465, -1.023, -23.405], dtype=float32)], ‘A-176-CA’: [array([ 29.751, -2.69 , -26.312], dtype=float32)]}…’

How do I add these two dictionaries together to get something like this:

print(avg_coords)
‘A-85-CA’: [array([ 81.858, -21.204, -5.5445], dtype=float32)], ‘A-86-CA’: [array([ 82.57, -21.447, -11.336], dtype=float32)]}…’

^ the keys are the same as helix1_coords, but the values are combined (I have added 50.393 + 31.465 for example)

I would then also like to divide each value by 2, so the final dictionary should look like this:

print(avg_coords)
‘A-85-CA’: [array([ 40.928, -10.602, 12.316], dtype=float32)], ‘A-86-CA’: [array([ 41.285,-10.7235, -5.668], dtype=float32)]}…’

Some things I tried:

avg_coords = {key: np.add(helix1_coords.values(), helix2_coords.values()) for key in helix1_coords}

^ This gave me a dictionary where all of the values were the same (the last value of helix1_coords)

avg_coords = {key: np.concatenate((helix1_coords.get(key, np.array([])), helix2_coords.get(key, np.array([])))) for key in set(helix1_coords.keys()).union(set(helix2_coords.keys()))}

^ This was suggested by another question but I got this error:

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)

Please help!

Asked By: ricehound

||

Answers:

To add two dictionaries together, you can use a for loop to iterate over the keys and values in each dictionary, adding the values together and storing the result in a new dictionary. Here is an example of how you can do this:

# Initialize the new dictionary
avg_coords = {}

# Iterate over the keys and values in the first dictionary
for key, value in helix1_coords.items():
    # Check if the key is also in the second dictionary
    if key in helix2_coords:
        # If the key is in both dictionaries, add the values together
        avg_coords[key] = value + helix2_coords[key]
    else:
        # If the key is only in the first dictionary, add the value to the new dictionary
        avg_coords[key] = value

# Iterate over the keys and values in the second dictionary
for key, value in helix2_coords.items():
    # Check if the key is not in the new dictionary
    if key not in avg_coords:
        # If the key is only in the second dictionary, add the value to the new dictionary
        avg_coords[key] = value

# Print the new dictionary
print(avg_coords)

This code will add the two dictionaries together to produce a new dictionary with the same keys as the input dictionaries, but with values that are the sum of the corresponding values in the input dictionaries.

To divide each value in the dictionary by 2, you can use a for loop to iterate over the keys and values in the dictionary and divide each value by 2. Here is an example of how you can do this:

# Iterate over the keys and values in the dictionary
for key, value in avg_coords.items():
    # Divide the value by 2 and update the value in the dictionary
    avg_coords[key] = value / 2

# Print the updated dictionary
print(avg_coords)

This code will divide each value in the dictionary by 2, updating the dictionary with the new values.

Answered By: user2988257

You can check the zip function, it helps you to iterate in parallel.
A quick example:

>>> l1 = [1,2,3,4]
>>> l2 = [4,3,2,1]
>>> for v1, v2 in zip(l1, l2):
        print(v1,v2)
1 4
2 3
3 2
4 1

In your specific case you can make a first zip with dict1 keys, dict1 values and dict2 values. Then you can iterate over the 2 lists with another zip and sum the elements together and divide by 2.

for k, v1, v2 in zip(helix1_coords, helix1_coords.values(), helix2_coords.values()):
    # list comprehension to create final list with the average value for every element
    avg_coords[k] = [(e1+e2)/2 for e1, e2 in zip(v1, v2)]

In one line you can use this, but it’s less readable:

avg_coords = {k: np.array([(e1+e2)/2 for e1, e2 in zip(v1, v2)]) for k, v1, v2 in zip(helix1_coords, helix1_coords.values(), helix2_coords.values())}
Answered By: Ricc
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.