Reputation: 99428
I have a list L
of dictionaries, where the dictionaries have the same set of keys.
I have another dictionary d
, which also has the same set of keys as the previous dictionaries.
For each key
in the set of keys, d[key]
is a function which can apply to L[i][key]
, i.e. d[key](L[i][key])
.
I want to reset the values of dictionaries in L
, using the functions in the values of d
, i.e.
L[i][key] = d[key](L[i][key])
for all key
and all i
.
How can I do it in a more pythonic way? Thanks.
E.g.
d = {'a':int, 'b':myfun, 'c':yourfun}
L[1] = {'a':`10`, 'b':5, 'c':1.4}
where int
is a type conversion function int()
, myfun
is a function which can take an integer as its argument, and yourfun
is another function which can take a floating number as its argument.
Upvotes: 2
Views: 1166
Reputation: 9850
You can regenerate L
with the correct values using a list and dict comprehension:
L = [{k: d[k](v) for (k, v) in l.iteritems()} for l in L]
Upvotes: 5