index of min element of sequence

Discussion in 'Python' started by Neal Becker, Jan 21, 2008.

  1. Neal Becker

    Neal Becker Guest

    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)
     
    Neal Becker, Jan 21, 2008
    #1
    1. Advertising

  2. Neal Becker

    Peter Otten Guest

    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
     
    Peter Otten, Jan 21, 2008
    #2
    1. Advertising

  3. Neal Becker

    Paul Rubin Guest

    Neal Becker <> 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]
     
    Paul Rubin, Jan 21, 2008
    #3
  4. Neal Becker

    Roger Miller Guest

    On Jan 21, 8:48 am, Peter Otten <> 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.
     
    Roger Miller, Jan 21, 2008
    #4
  5. Neal Becker

    John Machin Guest

    On Jan 22, 6:38 am, Paul Rubin <http://> wrote:
    > Neal Becker <> 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))
     
    John Machin, Jan 21, 2008
    #5
  6. Neal Becker

    Peter Otten Guest

    Roger Miller wrote:

    > On Jan 21, 8:48 am, Peter Otten <> 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
     
    Peter Otten, Jan 21, 2008
    #6
  7. Neal Becker

    Paul Rubin Guest

    John Machin <> 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()))
     
    Paul Rubin, Jan 21, 2008
    #7
  8. Neal Becker

    John Machin Guest

    On Jan 22, 7:56 am, Paul Rubin <http://> wrote:
    > John Machin <> 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)
     
    John Machin, Jan 21, 2008
    #8
  9. Neal Becker

    Paul Rubin Guest

    John Machin <> 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()))
     
    Paul Rubin, Jan 21, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lois
    Replies:
    1
    Views:
    3,241
    Ryan Stewart
    Dec 27, 2004
  2. juergen
    Replies:
    3
    Views:
    568
    opalinski from opalpaweb
    Sep 20, 2006
  3. Replies:
    7
    Views:
    363
    Alf P. Steinbach
    Feb 25, 2006
  4. Albert Hopkins

    When is min(a, b) != min(b, a)?

    Albert Hopkins, Jan 21, 2008, in forum: Python
    Replies:
    31
    Views:
    830
    Albert van der Horst
    Feb 4, 2008
  5. carmen

    Converting hrs and min to just min

    carmen, Aug 10, 2004, in forum: ASP General
    Replies:
    4
    Views:
    135
    carmen
    Aug 10, 2004
Loading...

Share This Page