Can I modify the keys in a dictionary directly for Python?

Dictionary key error python?

  • 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':… 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 A day is an object which has variables such as mean temperature, max temperature etc. Cat is the catagory 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?

  • Answer:

    oh my god it's HORRIBLE I don't see 'tempmax' used as a key anywhere in that code, I think it's cropped out of ' self.features.setdefault(cat,{f:{'high…

Sam at Yahoo! Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.