I need to compare dictionaries in Python and I wrote this code below:
diff_dict = {}
for key, value in old_dictionary.items():
for k, v in new_dictionry.items():
if k==key:
inner_dict = {}
for key1, value1 in value.items():
for k1, v1 in v.items():
if key1==k1 and value1!=v1:
compared_value = str(value1) + '_' + str(v1)
inner_dict.update({key1: compared_value})
diff_dict.update({k: inner_dict})
I have some sites as key, parameter as inner keys and value for each parameter. I need to compare the current dictionary with the dictionary from the previous day.
But it is not efficient, it takes half an hour and still, it's not done. Is there any efficient way to do this?
I want to get a new nested dict with a structure like this:
{key: {inner_key: compared_value}}
Compared value is concatenated new and old values so I can later split them by '_' and make two columns in Pandas.
example of data old:
{'site-1/cell-1': {'tac' : 10, md: 5},
'site-2/cell-1': {'tac' : 10, md: 1}}
new:
{'site-1/cell-1': {'tac' : 10, md: 4},
'site-2/cell-1': {'tac' : 12, md: 1}}
Desired output:
{{'site-1/cell-1': {md: '5_4'},
'site-2/cell-1': {'tac' : '10_12'}}