Search for mapping solution

Achim Domma domma at procoders.net
Sun Jul 6 15:36:17 EDT 2003


"Markus Joschko" <jocsch at phreaker.net> wrote in message
news:be9t66$2otne$1 at ID-47851.news.dfncis.de...
> 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'}

I would do it like this:

lines = [['fred','333','0.10'],['sam','444','1'],['fred','333','0.50']]
costs = {}
for name,number,price in lines:
    costs[name] = costs.setdefault(name,0)+float(price)
print costs

Achim






More information about the Python-list mailing list