return object from dictionary of nested lists when a condition (python) -
i have dictionary looks like:
myd={'key_0':[[['descrp_0_0'],obj_0_0],.....,[['descrp_0_n'],obj_0_n]] ,....., 'key_n':[[['descrp_n_0'],obj_n_0],.....,[['descrp_n_n'],obj_n_n]]}
all objs ndarrays of same shape , have function f() returns x float i.e.: obj_0_0.f() --> x_0_0
i want extract dictionary descrp , obj , respective key obj.f() (i.e. x) minimum values in each key (at myd scope of n keys give n items in shape of [descrp,obj]):
the result must like:
resd = {'key_0':[[descrp_0_min],obj_0_min], ....., 'key_n':[[descrp_n_min],obj_0_min]}
something like:
minxs = [min([item[-1].f() item in v]) k,v in myd.iteritems()] minobjs = [item k,v in myd.iteritems() item in v if item[-1].get_potential_energy() == minxs[myd.keys().index(k)]] resultlist = zip(myd.keys(),minobjs) resultdict = dict() in resultlist: resultdict[i[0]]=i[1]
although works rather cumbersome , think there must easier way this. or maybe should use numpy.ndarray purpose?
i appreciate , comments.
if i've understood structure of data correctly, think can solve dictionary comprehension calls builtin min
function , gives key
function.
results = {key: min(values, key=lambda x:x[-1].f()) key, values in myd.iteritems()}
your code close already!