index of min element of sequence
John Machin
sjmachin at lexicon.net
Mon Jan 21 15:06:19 EST 2008
On Jan 22, 6:38 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Neal Becker <ndbeck... at 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))
More information about the Python-list
mailing list