I just need some help in achieving to write logic for this prog:
Merge 2 objects with any depth (including contained dictionaries, lists, sets, strings, integers, floats). Type mismatches should yield a tuple with the two elements.
Examples:
a = {'x': [1,2,3], 'y': 1, 'z': set([1,2,3]), 'w': 'qweqwe', 't': {'a': [1, 2]}, 'm': [1]} b = {'x': [4,5,6], 'y': 4, 'z': set([4,2,3]), 'w': 'asdf', 't': {'a': [3, 2]}, 'm': "wer"}
Should produce:
{'x': [1,2,3,4,5,6], 'y': 5, 'z': set([1,2,3,4]), 'w': 'qweqweasdf', 't': {'a': [1, 2, 3, 2]}, 'm': ([1], "wer")}
As far as I did is looping over both dictionaries and checking the type of value in it and combining them as per its type.
for key in a.keys(): if type(a[key]) is type(b[key]): if type(a[key]) is list or type(a[key]) is int or type(a[key]) is str: lis = a[key] + b[key] dicty[key] = lis elif type(a[key]) is set: lis = list(a[key]) + list(b[key]) dicty[key] = set(lis) elif type(a[key]) is dict: # print func1(a[key],b[key]) dicty(a.items() + b.items()) else: dicty[key] = tuple(a[key]) + tuple(b[key]) return dicty
I believe there must be much better ways to solve this. I do not want the whole code, but some light on some other solution would be awesome. Thank you.