Run calculation multiple times with different values

Issue

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:

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.

Answered By – vaizki

Answer Checked By – Jay B. (AngularFixing Admin)

Leave a Reply

Your email address will not be published.