[Tutor] Addition, Dictionary, KeysError

Martin Walsh mwalsh at mwalsh.org
Sat Sep 13 22:52:02 CEST 2008


Rilindo Foster wrote:
> Scratch that, I'm a dork:
> 
> OrderDict[(o[0])] = OrderDict.get(o[0],0) + float(o[1])
> 
> http://www.faqts.com/knowledge_base/view.phtml/aid/4571/fid/541
> 
> :D
> 

For this case you might also be interested in collections.defaultdict,
added in python 2.5 I believe.

from collections import defaultdict

orders = defaultdict(float)
orders[o[0]] += float(o[1])

HTH,
Marty




More information about the Tutor mailing list