Search for mapping solution

Mike C. Fletcher mcfletch at rogers.com
Mon Jul 7 00:18:20 CEST 2003


Since you know the indices, you can just do this:

for record in lines:
    costs[record[nameIndex]] = 
costs.setdefault(record[nameIndex],0)+float(record[priceIndex])

Enjoy,
Mike

Markus Joschko wrote:

>>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
>  
>
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list