Search for mapping solution

Markus Joschko jocsch at phreaker.net
Sun Jul 6 16:13:39 EDT 2003


> 
> 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
> 
thanks it works. But maybe I can complicate the example a little bit
(because in real world it's more complicated):

What if I every list in lines has 20 or more entries and I have only the
index number to access the name, e.g.

lines = [['elem1','elem2','fred','elem3',.......;'elem
17','333','elem18','0.10'],[...],[...]]


what I want to say: I can't be sure that the name is always on the third
position. That's dynamic. I know it before I parse the list, but 
I can't say 

for elem1,elem2,name,.... cause it can also be

for elem1,name,elem3 ....


Thanks for the answer,
        Markus




More information about the Python-list mailing list