index of min element of sequence

N

Neal Becker

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)
 
P

Paul Rubin

Neal Becker said:
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]
 
R

Roger Miller

....

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.
 
J

John Machin

Neal Becker said:
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))
 
P

Peter Otten

Roger said:
...

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
 
P

Paul Rubin

John Machin said:
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()))
 
J

John Machin

John Machin said:
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!
(7, 3)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top