[Tutor] Addition, Dictionary, KeysError

Rilindo Foster webmaster at monzell.com
Sat Sep 13 22:05:37 CEST 2008


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 <webmaster at monzell.com>
> Date: September 13, 2008 3:53:34 PM EDT
> To: Tutor at 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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080913/bc2da4f3/attachment.htm>


More information about the Tutor mailing list