Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > index of min element of sequence

Reply
Thread Tools

index of min element of sequence

 
 
Neal Becker
Guest
Posts: n/a
 
      01-21-2008
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)

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-21-2008
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
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      01-21-2008
Neal Becker <(E-Mail Removed)> 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]
 
Reply With Quote
 
Roger Miller
Guest
Posts: n/a
 
      01-21-2008
On Jan 21, 8:48 am, Peter Otten <(E-Mail Removed)> 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.



 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-21-2008
On Jan 22, 6:38 am, Paul Rubin <http://(E-Mail Removed)> wrote:
> Neal Becker <(E-Mail Removed)> 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))
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      01-21-2008
Roger Miller wrote:

> On Jan 21, 8:48 am, Peter Otten <(E-Mail Removed)> 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
 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-21-2008
John Machin <(E-Mail Removed)> 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()))
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      01-21-2008
On Jan 22, 7:56 am, Paul Rubin <http://(E-Mail Removed)> wrote:
> John Machin <(E-Mail Removed)> 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)

 
Reply With Quote
 
Paul Rubin
Guest
Posts: n/a
 
      01-21-2008
John Machin <(E-Mail Removed)> 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()))
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When is min(a, b) != min(b, a)? Albert Hopkins Python 31 02-04-2008 07:37 PM
How to write min(a,b) instead of Math.min(a,b) juergen Java 3 09-20-2006 04:20 AM
Find out the index of a vector which has the min element Plissken.s@gmail.com C++ 7 02-25-2006 01:28 AM
CSS min-width, max-width, and min-height with display:inline Lois HTML 1 12-27-2004 03:03 AM
Converting hrs and min to just min carmen ASP General 4 08-10-2004 08:31 PM



Advertisments