Search for mapping solution

Mike C. Fletcher mcfletch at rogers.com
Sun Jul 6 23:21:00 CEST 2003


result = {}
for (name, whatever, costs) in lines:
    costs = float(number.replace(',','.'))
    dict[name] = dict.get( name, 0.0) + costs

(that's untested, but you should get the idea).  Note, however, floating 
point is generally a poor choice for accounting applications, so you may 
want to look into the libraries for fixed-point calculations.

HTH,
Mike

Markus Joschko wrote:

>Hi,
>stated in a post befor, I'm a java programmer, fascinated about the elegant
>way python solves iterations. Maybe you can show me a solution how to map
>the following
>
>I have a List:
>
>Name - Number - Costs
>
>lines = [['fred','333','0,10'],['sam','444','1'],['fred','333','0,50']]
>
>Now I want to have it in a dictionary(name,costs)  Should look like
>{'fred':'0,60' , 'sam':'1'}
>
>What's an elegant way to do it? I can use a lot of loops, but I assume, that
>there is a better way of doing so.
>
>Thanks, 
> Markus
>  
>
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list