5 Replies - 8281 Views - Last Post: 23 March 2011 - 01:31 PM Rate Topic: -----

#1 srose34   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 10-October 09

dictionary key error

Posted 23 March 2011 - 10:40 AM

Hi I get the error "Key Error: 'tempMax'" Can anyone tell what the problem is with the following code:

def catagorise(self, day, cat, f):
		self.features.setdefault(cat,{f:{'high':0,'mid':0,'low':0}})

		if f == 'tempMean':
			if day.tempMean > 15.0:
				self.features[cat][f]['high']+=1
			elif day.tempMean >8.0 and day.tempMean < 15.0:
				self.features[cat][f]['mid']+=1
			elif day.tempMean <=8.0:
				self.features[cat][f]['low']+=1		

		if f == 'tempMax':
			if day.tempMax > 15.0:
				self.features[cat][f]['high']+=1
			elif day.tempMax >8.0 and day.tempMax < 15.0:
				self.features[cat][f]['mid']+=1
			elif day.tempMax <=8.0:
				self.features[cat][f]['low']+=1		

		if f == 'tempMin':
			if day.tempMin > 15.0:
				self.features[cat][f]['high']+=1
			elif day.tempMin >8.0 and day.tempMin < 15.0:
				self.features[cat][f]['mid']+=1
			elif day.tempMin <=8.0:
				self.features[cat][f]['low']+=1		

		if f == 'dewPoint':
			if day.dewPoint > 15.0:
				self.features[cat][f]['high']+=1
			elif day.dewPoint >8.0 and day.dewPoint < 15.0:
				self.features[cat][f]['mid']+=1
			elif day.dewPoint <=8.0:
				self.features[cat][f]['low']+=1	


A day is an object which has variables such as mean temperature, max temperature etc. Cat is the category which it will be put into e.g 'Fog', 'Rain', 'Snow', 'None' and f is the feature to check e.g. 'tempMax'

The features dictionary is defined when the class is created.

How do I get the dictionary to add the keys and values automatically?

Is This A Good Question/Topic? 0
  • +

Replies To: dictionary key error

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,259
  • Joined: 23-December 08

Re: dictionary key error

Posted 23 March 2011 - 10:49 AM

Here's some documentation on Key Errors. Long story short, you're looking for a key that's not in the dictionary. As a rule, could you please copy and paste the entire error when you need help on it? It'll help us to help you :)

So, you need to add a value to your dictionary dynamically? That's not hard. The syntax isdictionary[key] = value. You can make a little function to make it cleaner if you'd like, but it's really not necessary:
def addToDictionary(dictionary,key,value):
	dictionary[key] = value


Does this help?
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: dictionary key error

Posted 23 March 2011 - 11:05 AM

If you're doing something like this:
self.features[cat][f]['high']+=1

and there is no existing value at self.features[cat][f]['high'] (or one of the dicts along the way), you're going to run into this. You're telling the interpreter to go to self.features[cat][f]['high'] in the dictionary, retrieve the value there and add 1 to it...but if there's no value there, what is Python to do?
Was This Post Helpful? 1
  • +
  • -

#4 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 322
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: dictionary key error

Posted 23 March 2011 - 11:18 AM

As well as what atraub suggested, you might want to use the .get() method on a dictionary.

>>> dict = {'maxTemp':45, 'windSpeed':100, 'windDirection':'N'}
>>> dict.get('windSpeed', 'Not defined')
100
>>> dict.get('maxTemp', 'Not defined')
45
>>> dict.get('minTemp', 'Not defined')
Not defined



Hopefully it's clear, if they key is in the dictionary it retrieves the value of the key, if not, the method returns the default value, in this case 'Not defined', there is nothing stopping you having different defaults for each case, whatever makes the most sense. :)
Was This Post Helpful? 2
  • +
  • -

#5 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,259
  • Joined: 23-December 08

Re: dictionary key error

Posted 23 March 2011 - 11:19 AM

I didn't know about that one, clever clever Simown.
Was This Post Helpful? 0
  • +
  • -

#6 Simown   User is offline

  • Blue Sprat
  • member icon

Reputation: 322
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: dictionary key error

Posted 23 March 2011 - 01:31 PM

Thanks atraub! Means a lot from you. I guess you know it or you don't, but hey, I'll take the compliment :smartass:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1