Search for mapping solution

Alan Kennedy alanmk at hotmail.com
Sun Jul 6 15:54:45 EDT 2003


Markus Joschko wrote:

> 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?

Must ..... resist ..... temptation ..... to ..... write .... oneliners

D'oh! I'm so weak willed....

Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 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'}
>>>

OK, I'll write and maintain a 1000-line perl program as penance ...

sometimes-concise-is-elegant-too-ly yrs.

-- 
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan




More information about the Python-list mailing list