I am trying to do arithmetic operations between two Ordered dictionaries in Python. However I am facing the error:
TypeError: unsupported operand type(s) for -: 'collections.OrderedDict' and 'int'
Below is the Python code:
from collections import OrderedDict
a = {'a_key':OrderedDict([('Max_Value', [2000]), ('Min_Value', [1000])])}
b = {'b_key':OrderedDict([('Max_Value', [1000]), ('Min_Value', [50])])}
test_dict = {key: str(round(((a[key] - b.get(key, 0))/b.get(key, 0))*100,2))+'%'
for key in a.keys()}
print(test_dict)
Expected result: The final outcome should be assigned to test_dict. Here we want to find the percentage difference between 'Max_Value' of each provided above dictionaries (a, b). Similarly for 'Min_Value'. Both 'Max_Value' and 'Min_Value' are inside OrderedDict of the dictionaries a, b.
Expected test_dict = {'c_key': OrderedDict([('Max_Value', ['100.0%']), ('Min_Value', ['1900.0%'])])}
After long struggle, I have found the desired solution by myself, on exploring a bit more. Below is the code :
Output: