Search for mapping solution

Sean Ross sross at connectmail.carleton.ca
Sun Jul 6 16:18:45 EDT 2003


"Alan Kennedy" <alanmk at hotmail.com> wrote in message
news:3F087E85.9D8018E9 at hotmail.com...
> >>> lines =
[['fred','333','0,10'],['sam','444','1'],['fred','333','0,50']]
> >>> d = dict([(x[0], x[2]) for x in lines])
> >>> d
> {'sam': '1', 'fred': '0,50'}
> >>>

Hi.  You've left out the accumulating part of the OP's requirements:

lines = [['fred','333','0.10'],['sam','444','1'],['fred','333','0.50']]
costs = {}
# nearly identical to Achim's solution, but with a list comp.
[costs.__setitem__(name, costs.get(name,0)+float(price))
         for name, number, price in lines]
print costs
# outputs: {'sam': 1.0, 'fred': 0.60}






More information about the Python-list mailing list