Re: Index of maximum element in list

2008-01-27 Thread Arnaud Delobelle
On Jan 27, 11:32 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: [...] > > simple_posmax is more than 3x faster on my machine.  It's not > surprising as even though the list is walked twice, it is all done in > C and no new objects have to be created. Then only non-C bit is when > the result of ma

Re: Index of maximum element in list

2008-01-27 Thread Arnaud Delobelle
On Jan 27, 11:42 am, Paul Rubin wrote: > Arnaud Delobelle <[EMAIL PROTECTED]> writes: > > def simple_posmax(l, key=None): > >     if key: > >         return l.index(max(l, key=key)) > >     else: > >         return l.index(max(l)) > > def simple_posmax(l, **kw): >    retu

Re: Index of maximum element in list

2008-01-27 Thread Paul Rubin
Arnaud Delobelle <[EMAIL PROTECTED]> writes: > def simple_posmax(l, key=None): > if key: > return l.index(max(l, key=key)) > else: > return l.index(max(l)) def simple_posmax(l, **kw): return l.index(max(l, **kw)) > simple_posmax is more than 3x faster on my machine. I

Re: Index of maximum element in list

2008-01-27 Thread Arnaud Delobelle
On Jan 26, 10:07 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Sat, 26 Jan 2008 12:40:26 -0800, Paul Rubin wrote: > > [EMAIL PROTECTED] writes: > >> > def posmax(seq, key=lambda x:x): > >> >    return max(enumerate(seq), key=lambda k: key(k[1]))[0] > > >> Is the Python max

Re: Index of maximum element in list

2008-01-27 Thread Paul Rubin
[EMAIL PROTECTED] writes: > The final sum: the Psyco version is almost 40 times faster than the > Python version, and just 2.8 times slower than the D version, and 3.4 > times slower than a C version (that doesn't use too many pointer > tricks). I think this is good enough. I can't help wishing th

Re: Index of maximum element in list

2008-01-27 Thread bearophileHUGS
bearophile: > That version is easy to translate to other languages and you can > probably find that Psyco makes it much faster still. That operation is quite common, so it deserves a bit more work: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/543271 (I can show you the D/C code if you

Re: Index of maximum element in list

2008-01-26 Thread Paul Rubin
Steven D'Aprano <[EMAIL PROTECTED]> writes: > Actually there is a big speed hit. Until such time that Python has a fast > identity function, and lambda x:x isn't it, your version is about three > times slower than Bearophile's. Interesting. Fixing that speed hit probably needs compiler attentio

Re: Index of maximum element in list

2008-01-26 Thread bearophileHUGS
Steven D'Aprano: > Much to my surprise, the fastest solution I've tried appears to be a pure > Python version not even using max() at all. That version is easy to translate to other languages and you can probably find that Psyco makes it much faster still. Bye, bearophile -- http://mail.python.o

Re: Index of maximum element in list

2008-01-26 Thread Steven D'Aprano
On Sat, 26 Jan 2008 12:40:26 -0800, Paul Rubin wrote: > [EMAIL PROTECTED] writes: >> > def posmax(seq, key=lambda x:x): >> >return max(enumerate(seq), key=lambda k: key(k[1]))[0] >> >> Is the Python max able to tell that's the identity function? I don't >> think so, so your version may be slo

Re: Index of maximum element in list

2008-01-26 Thread Paul Rubin
[EMAIL PROTECTED] writes: > > def posmax(seq, key=lambda x:x): > >return max(enumerate(seq), key=lambda k: key(k[1]))[0] > > Is the Python max able to tell that's the identity function? I don't > think so, so your version may be slower and more memory hungry in the > common situation where you

Re: Index of maximum element in list

2008-01-26 Thread bearophileHUGS
Paul Rubin: > def posmax(seq, key=lambda x:x): >return max(enumerate(seq), key=lambda k: key(k[1]))[0] Is the Python max able to tell that's the identity function? I don't think so, so your version may be slower and more memory hungry in the common situation where you don't have a key function

Re: Index of maximum element in list

2008-01-26 Thread Paul Rubin
[EMAIL PROTECTED] writes: > def posmax(seq, key=None): > """Return the position of the first maximum item of a sequence. > It accepts the usual key parameter too.""" > if key: > return max(enumerate(seq), key=lambda k: key(k[1]))[0] > else: > return max(enumerate(seq

Re: Index of maximum element in list

2008-01-26 Thread bearophileHUGS
> Henry Baxter wrote: > > def maxi(l): > > m = max(l) > > for i, v in enumerate(l): > > if m == v: > > return i > > What's about l.index(max(l)) ? The version I use: def posmax(seq, key=None): """Return the position of the first maximum item of a sequence. It a

Re: Index of maximum element in list

2008-01-25 Thread Paul Rubin
"Terry Reedy" <[EMAIL PROTECTED]> writes: > | > What's about l.index(max(l)) ? > > Both of these methods scan the list twice. The two given by RH and SDD do > so just once. Both of those will give the index of the of the last maximum > value. If you want the index of the first max value (you

Re: Index of maximum element in list

2008-01-25 Thread Terry Reedy
"Henry Baxter" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | Thanks Hexamorph and Neal. Somehow I didn't make the connection with using | 'index', but I'm all sorted out now :) | | On Jan 25, 2008 1:47 PM, Hexamorph <[EMAIL PROTECTED]> wrote: | | > Henry Baxter wrote: | > > Oops,

Re: Index of maximum element in list

2008-01-25 Thread Scott David Daniels
Hexamorph wrote: > ... > What's about l.index(max(l)) ? What about max((x,i) for i,x in enumerate(lst))[1] -- http://mail.python.org/mailman/listinfo/python-list

Re: Index of maximum element in list

2008-01-25 Thread Raymond Hettinger
On Jan 25, 1:47 pm, Hexamorph <[EMAIL PROTECTED]> wrote: > Henry Baxter wrote: > > Oops, gmail has keyboard shortcuts apparently, to continue: > > > def maxi(l): > >     m = max(l) > >     for i, v in enumerate(l): > >         if m == v: > >             return i > > What's about l.index(max(l)) ?

Re: Index of maximum element in list

2008-01-25 Thread Henry Baxter
Thanks Hexamorph and Neal. Somehow I didn't make the connection with using 'index', but I'm all sorted out now :) On Jan 25, 2008 1:47 PM, Hexamorph <[EMAIL PROTECTED]> wrote: > Henry Baxter wrote: > > Oops, gmail has keyboard shortcuts apparently, to continue: > > > > def maxi(l): > > m = ma

Re: Index of maximum element in list

2008-01-25 Thread Neal Becker
Henry Baxter wrote: > Oops, gmail has keyboard shortcuts apparently, to continue: > > def maxi(l): > m = max(l) > for i, v in enumerate(l): > if m == v: > return i > > But it seems like something that should be built in - or at least I should > be able to write a lamb

Re: Index of maximum element in list

2008-01-25 Thread Hexamorph
Henry Baxter wrote: > Oops, gmail has keyboard shortcuts apparently, to continue: > > def maxi(l): > m = max(l) > for i, v in enumerate(l): > if m == v: > return i > What's about l.index(max(l)) ? -- http://mail.python.org/mailman/listinfo/python-list

Re: Index of maximum element in list

2008-01-25 Thread Henry Baxter
Oops, gmail has keyboard shortcuts apparently, to continue: def maxi(l): m = max(l) for i, v in enumerate(l): if m == v: return i But it seems like something that should be built in - or at least I should be able to write a lambda function for it, but I'm not sure how