Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   index of min element of sequence (http://www.velocityreviews.com/forums/t586227-index-of-min-element-of-sequence.html)

 Neal Becker 01-21-2008 06:28 PM

index of min element of sequence

What's a good/fast way to find the index of the minimum element of a
sequence? (I'm fairly sure sorting the sequence is not the fastest
approach)

 Peter Otten 01-21-2008 06:48 PM

Re: index of min element of sequence

Neal Becker wrote:

> What's a good/fast way to find the index of the minimum element of a
> sequence? (I'm fairly sure sorting the sequence is not the fastest
> approach)

>>> items = "defbhkamnz"
>>> min(xrange(len(items)), key=items.__getitem__)

6
>>> items[6]

'a'

Peter

 Paul Rubin 01-21-2008 07:38 PM

Re: index of min element of sequence

Neal Becker <ndbecker2@gmail.com> writes:
> What's a good/fast way to find the index of the minimum element of a
> sequence? (I'm fairly sure sorting the sequence is not the fastest
> approach)

Python 2.5 (untested):

from operator import itemgetter
minindex = min(enumerate(seq), key=itemgetter(1))[1]

 Roger Miller 01-21-2008 07:47 PM

Re: index of min element of sequence

On Jan 21, 8:48 am, Peter Otten <__pete...@web.de> wrote:

> Neal Becker wrote:
> > What's a good/fast way to find the index of the minimum element of a
> > sequence?

....

> >>> min(xrange(len(items)), key=items.__getitem__)

....

Or just
items.index(min(items))
I found this to be significantly faster in a simple test (searching a
list of 1000 ints with the minimum in the middle), despite the fact
that it requires two passes. I'm sure that one could find cased where
Peter's approach is faster, so you if you are concerned about speed
you should measure with your own data.

 John Machin 01-21-2008 08:06 PM

Re: index of min element of sequence

On Jan 22, 6:38 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Neal Becker <ndbeck...@gmail.com> writes:
> > What's a good/fast way to find the index of the minimum element of a
> > sequence? (I'm fairly sure sorting the sequence is not the fastest
> > approach)

>
> Python 2.5 (untested):
>
> from operator import itemgetter
> minindex = min(enumerate(seq), key=itemgetter(1))[1]

Bzzzt!

>>> seq = [1000, 9, 8, 7, 2000, 3000]
>>> from operator import itemgetter
>>> minindex = min(enumerate(seq), key=itemgetter(1))[1]
>>> minindex

7
>>> min(enumerate(seq), key=itemgetter(1))

(3, 7)

s/[1]/[0]/ or more generally:

minindex, minvalue = min(enumerate(seq), key=itemgetter(1))

 Peter Otten 01-21-2008 08:28 PM

Re: index of min element of sequence

Roger Miller wrote:

> On Jan 21, 8:48 am, Peter Otten <__pete...@web.de> wrote:
>
>> Neal Becker wrote:
>> > What's a good/fast way to find the index of the minimum element of a
>> > sequence?

> ...
>
>> >>> min(xrange(len(items)), key=items.__getitem__)

> ...
>
> Or just
> items.index(min(items))
> I found this to be significantly faster in a simple test (searching a
> list of 1000 ints with the minimum in the middle), despite the fact
> that it requires two passes. I'm sure that one could find cased where
> Peter's approach is faster, so you if you are concerned about speed
> you should measure with your own data.

I suppose those cases are rare (slow equality check), and even then I
might prefer your solution because it's so much clearer.

Peter

 Paul Rubin 01-21-2008 08:56 PM

Re: index of min element of sequence

John Machin <sjmachin@lexicon.net> writes:
> s/[1]/[0]/ or more generally:

Oops, got two spellings confused. Originally was going to use

from itertools import count, izip
min(izip(seq, count()))[1]

but did it with enumerate instead. I don't know which is actually
faster.

> minindex, minvalue = min(enumerate(seq), key=itemgetter(1))

Cool, I like this best of all. Or alternatively,

minindex, minvalue = min(izip(seq, count()))

 John Machin 01-21-2008 09:47 PM

Re: index of min element of sequence

On Jan 22, 7:56 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> John Machin <sjmac...@lexicon.net> writes:
> > s/[1]/[0]/ or more generally:

>
> Oops, got two spellings confused. Originally was going to use
>
> from itertools import count, izip
> min(izip(seq, count()))[1]
>
> but did it with enumerate instead. I don't know which is actually
> faster.
>
> > minindex, minvalue = min(enumerate(seq), key=itemgetter(1))

>
> Cool, I like this best of all. Or alternatively,
>
> minindex, minvalue = min(izip(seq, count()))

Bzzzt #2!

>>> from itertools import count, izip
>>> min(izip(seq, count()))

(7, 3)

 Paul Rubin 01-21-2008 09:53 PM

Re: index of min element of sequence

John Machin <sjmachin@lexicon.net> writes:
> >>> from itertools import count, izip
> >>> min(izip(seq, count()))

> (7, 3)

Serves me right for cutting and pasting.

minvalue, minindex = min(izip(seq, count()))

 All times are GMT. The time now is 01:43 PM.