Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Performing mathematical operations on values from two dictionaries with same keys in Python

I have two dictionaries named as, lets say, a = {'A':{2021:45.65},'B':{2021:56.34}} and b = {'A':{2021:78.67},'B':{2021:87.54}}

I want to get the values from both A and B ‘sub-dictionaries’ and compute what percentage of value from the second dictionary is the value from the first dictionary.

I can’t seem to figure out a way how to access both the float values from different dictionaries and compute the result.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

————————–UPDATE————————

Apologies about not comprehending my problem in a proper manner. I figured out a way to do that and if you think it has an issue still please do mention.

dict_keys = ['A','B']
for x in dict_keys:
   val1 = a[x].values()
   val2 = b[x].values()

>Solution :

As has been mentioned in some of the comments breaking this into logical parts/steps can help a lot.

//pseudocode
if the len of a == len of b:
    for key, value in a:
        if the len of a[key] == len of b[key]:
            for key, value in a[key]:
                more_less = a's key's value divided by b's key's value

Write it out, if you get an error, post it and I will post help / more code. I will also edit this with proper code after OP gets this figured out, so please don’t downvote this in the meantime. I am doing this for OP and will make an evergreen version for anyone reading this down the line. Thanks.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading