find minimum associated values

Alan Isaac aisaac at american.edu
Fri Jan 25 17:30:30 EST 2008


Alan Isaac wrote:
 
> #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.

On average.
For the specified problem.
;-)



More information about the Python-list mailing list