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

Run calculation multiple times with different values

I have two dictionaries, that look like:

dict1 = {1: 10, 2: 23, .... 999: 12}

dict2 = {1: 42, 2: 90, .... 999: 78}

I want to perform a simple calculation: Multiply value of dict1 with value of dict2 for 1 and 2 each.

The code so far is:

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

dict1[1] * dict2[1]

This calculates 10*42, which is exactly what i want.

Now i want to perform this calculation for every index in the dictionary, so for 1 up to 999.

I tried:

i = {1,2,3,4,5,6 ... 999}

dict1[i] * dict2[i]

But it didnt work.

>Solution :

This creates a new dict with the results:

out = { i: dict1[i] * dict2[i] for i in range(1,1000) }

If you need to work with vectors and matrices take a look at the numpy module. It has data structures and a huge collection of tools for working with them.

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