lara
lara

Reputation: 123

Python: Iterating through multiple dictionaries

I've 3 lists, each of them contains dictionaries. I would get the values for certain keys and want to save the result from the min-Operation of this values. The following code gives a error: TypeError: list indices must be integers, not dict.

import numpy as np
import itertools


humidity = [{'high': 90, 'middle': 50, 'low': 10}, {'high': 75, 'middle': 40, 'low': 5} ]

temperature =[{'warm': 35, 'normal': 20, 'cold': 5 }, {'warm': 40, 'normal': 18, 'cold': 10 }]

wind = [{'breeze': 25, 'gale': 100}, {'breeze': 20, 'gale': 90}]

results=[]
for h, t, w in itertools.izip(humidity, temperature, wind):

  results.append(np.fmin (humidity[h]['high'], np.fmin( temperature[t]['normal'], wind[w]['breeze']) ) )

But if i wrote in the console(Spyder,python 2.7):

 result = np.fmin (humidity[0]['high'], np.fmin( temperature[0]['normal'], wind[0]['breeze']) )

I get 20 and it's true. But why can I not iterate over the whole dictionaries? What is wrong?

Thank you very much for your answers/ideas!

Upvotes: 0

Views: 1635

Answers (1)

C.B.
C.B.

Reputation: 8346

You need to reference your h,t,w variables instead of the original lists

results.append(np.fmin (h['high'], np.fmin( t['normal'], w['breeze']) ) )

as h,t,w are dictionaries unpacked from your zip, not ints so you can't use them to index a list

Visiually, humidity[h]['high'] looks like

humidity[{'high': 90, 'middle': 50, 'low': 10}]['high']

Which does not make sense syntactically

Upvotes: 1

Related Questions