Re: find minimum associated values

2008-01-25 Thread Alan Isaac
[EMAIL PROTECTED] wrote: > I'd use the first solution. It can be speeded up a bit with a try/except: for k,v in kv: try: if d[k] > v: d[k] = v except KeyError: d[k] = v Cheers, Alan Isaac -- http://mail.python.org/mailman/listinfo/python-list

Re: find minimum associated values

2008-01-25 Thread Alan Isaac
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

Re: find minimum associated values

2008-01-25 Thread Alan Isaac
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() fo

Re: find minimum associated values

2008-01-25 Thread John Machin
On Jan 26, 7:58 am, [EMAIL PROTECTED] wrote: > I find the first and third solutions simpler to read, and the first > solution requires less memory, it probably works quite well with > Psyco, and it's easy to translate to other languages (that is > important for programs you want to use for a lot of

Re: find minimum associated values

2008-01-25 Thread bearophileHUGS
I find the first and third solutions simpler to read, and the first solution requires less memory, it probably works quite well with Psyco, and it's easy to translate to other languages (that is important for programs you want to use for a lot of time or in different situations), so I'd use the fir

Re: find minimum associated values

2008-01-25 Thread Paul Rubin
Alan Isaac <[EMAIL PROTECTED]> writes: > print "method 2: groupby" > t=time.clock() > d = dict() > kv_sorted = sorted(kv, key=lambda x: id(x[0])) How about something like: kv_sorted = sorted(kv, key=lambda x: (id(x[0]), x[1])) Now do your groupby and the first element of each group is the mini

Re: find minimum associated values

2008-01-25 Thread Alan Isaac
Steven Bethard wrote: > [3rd approach] Seems "pretty" enough to me. ;-) I find it most attractive of the lot. But its costs would rise if the number of values per object were large. Anyway, I basically agree. Thanks, Alan -- http://mail.python.org/mailman/listinfo/python-list

Re: find minimum associated values

2008-01-25 Thread Steven Bethard
Alan Isaac wrote: > I have a small set of objects associated with a larger > set of values, and I want to map each object to its > minimum associated value. The solutions below work, > but I would like to see prettier solutions... > [snip] > > # arbitrary setup > keys = [Pass() for i in range(10)]

find minimum associated values

2008-01-25 Thread Alan Isaac
I have a small set of objects associated with a larger set of values, and I want to map each object to its minimum associated value. The solutions below work, but I would like to see prettier solutions... Thank you, Alan Isaac ===