Python- Convert dictionary to dictionary with list value -
i wanna convert dictionary dictionary list value
i wanna convert :
f= {24149423: 59, 22621293.0: 367562.0, 24144369.0: 405017.0, 22662739.0: 276484.0, 22470388.0: 410315.0, 22624054.0: 398444.0, 22614201.0: 380106.0, 22564857.0: 90756.0, 22619259.0: 382386.0, 22692381.0: 388392.0}
to dictionary list value
b = {90756.0: [22564857.0], 388392.0: [22692381.0], 367562.0: [22621293.0], 410315.0: [22470388.0], 398444.0: [22624054.0], 382386.0: [22619259.0], 405017.0: [24144369.0], 276484.0: [22662739.0], 59: [24149423], 380106.0: [22614201.0]}
i try write way:
from collections import defaultdict b = defaultdict(list) key, value in sorted(f.iteritems()): b[value].append(key)
i used map well.
b = map(lambda k, v: f.update({k: v}), keys, values)
but none of these work.
use dict comprehension, this:
b = { v: [k] k,v in f.items() }
[k]
one-element list containing key, v
value, plays rold of key in resulting dictionary.
Comments
Post a Comment