find minimum associated values

Alan Isaac aisaac at american.edu
Fri Jan 25 23:28:51 CET 2008


Paul Rubin wrote: 
> How about something like:
> 
>   kv_sorted = sorted(kv, key=lambda x: (id(x[0]), x[1]))


You mean like this?

#sort by id and then value
kv_sorted = sorted(kv, key=lambda x: (id(x[0]),x[1]))
#groupby: first element in each group is object and its min value
d =dict( g.next() for k,g in groupby( kv_sorted, key=lambda x: x[0] ) )

Yes, that appears to be fastest and is
pretty easy to read.

Thanks,
Alan



More information about the Python-list mailing list