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


From: Rilindo Foster <[EMAIL PROTECTED]>
Date: September 13, 2008 3:53:34 PM EDT
To: Tutor@python.org
Subject: Addition, Dictionary, KeysError

Another silly question (if you were to call it a question)

Pulling the list of Store Orders and Sales and I am attempting to accumulate the total amount of sales per Store.

In Perl, if I were to iterate through a hash, I can easily add the values and populate the hash with:

TotalSales{Store} = TotalSales{Store} + Sales

In Python, it won't let me do that. If I were to do what I have done in Perl:

OrderDict[o[0]] = float(OrderDict[o[0]]) + float(o[1])

I kept getting a "KeyError".

Am I doing this right? For some reason, I think I know the answer, but I can't put my finger on it.

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to