Reputation: 277
I have a dictionary in this format:
{
'a': ([1,2,3],[-1,-2,-3]),
'b': ([1,2,3],[-1,-2,-3]),
'z': ([],[-1])
}
And another one:
{
'a': ([4,5],[]),
'c': ([1,2,3],[-4]),
'z': ([],[-3])
}
I would like to combine both of them into this format:
{
'a': ([1,2,3,4,5],[-1,-2,-3]),
'b': ([1,2,3],[-1,-2,-3]),
'c': ([1,2,3],[-4]),
'z': ([],[-1,-3])
}
How would I go about doing this? I'm also fairly new to Python and I'm wondering if this would be the best way of representing my data and if there is some data structure I should be using instead of a dictionary.
Upvotes: 0
Views: 89
Reputation: 6935
Try this :
d1 = {'a': ([1,2,3],[-1,-2,-3]),'b': ([1,2,3],[-1,-2,-3]),'z': ([],[-1])}
d2 = {'a': ([4,5],[]),'c': ([1,2,3],[-4]),'z': ([],[-3])}
d3 = {}
d4 = {**d1, **d2} # This will work for Python 3.5+
for k in d4:
if k in d2 and k in d1:
tm = (d1[k][0]+ d2[k][0], d1[k][1]+d2[k][1])
d3[k] = tm
elif k in d2 and k not in d1:
d3[k] = d2[k]
else:
d3[k] = d1[k]
OUTPUT :
d3 = {'a': ([1, 2, 3, 4, 5], [-1, -2, -3]), 'b': ([1, 2, 3], [-1, -2, -3]), 'z': ([], [-1, -3]), 'c': ([1, 2, 3], [-4])}
Upvotes: 2
Reputation: 6246
You can break this problem into two steps, using a helper function that handles the merging.
dict1 = {
'a': ([1,2,3],[-1,-2,-3]),
'b': ([1,2,3],[-1,-2,-3]),
'z': ([],[-1])
}
dict2 = {
'a': ([4,5],[]),
'c': ([1,2,3],[-4]),
'z': ([],[-3])
}
def ordered_list_merge(lst1, lst2):
'''
Merges two lists, and does not add duplicates from lst2 into lst1
'''
resulting_list = lst1.copy() #to ensure list 1 is not mutated by changes to resulting_list
resulting_list.extend(x for x in lst2 if x not in resulting_list)
return resulting_list
import copy
result_dict = copy.deepcopy(dict1) #to ensure result_dict is an entirely new object, and mutations on result_dict do not affect dict1
for k, v in dict2.items():
if k not in result_dict:
result_dict[k] = v
else:
result_dict[k] = tuple(ordered_list_merge(lst1, lst2)
for lst1, lst2 in
zip(result_dict[k], v))
print(result_dict)
#Output:
{'a': ([1, 2, 3, 4, 5], [-1, -2, -3]),
'b': ([1, 2, 3], [-1, -2, -3]),
'z': ([], [-1, -3]),
'c': ([1, 2, 3], [-4])}
Note that dictionaries are inherently unordered (or remember insertion order in python 3.7+) and should not be relied on for order. Use a sort to get a list of tuples, or use an OrderedDict if the order is also important.
Upvotes: 1