Friday, 15 July 2011

python - From a list of lists to a dictionary -



python - From a list of lists to a dictionary -

i have list of lists specific structure, looks following:

lol = [['geo','ds_11',45.3,90.1,10.2],['geo','ds_12',5.3,0.1,0.2],['mao','ds_14',12.3,90.1,1],...]

i'd transform lol (list of lists) dictionary of next form (note sec element of each list in lol supposed unique, key dict:

dict_lol = {'ds_11': ['geo','ds_11',45.3,90.1,10.2], 'ds_12':['geo','ds_12',5.3,0.1,0.2], 'ds_14':['mao','ds_14',12.3,90.1,1],...}

i loop looking more elegant pythonic way it.

thanks!

use dictionary comprehension, available in python 2.7+

in [93]: {x[1]:x x in lol} out[93]: {'ds_11': ['geo', 'ds_11', 45.3, 90.1, 10.2], 'ds_12': ['geo', 'ds_12', 5.3, 0.1, 0.2], 'ds_14': ['mao', 'ds_14', 12.3, 90.1, 1]}

python dictionary

No comments:

Post a Comment