Max function question: How do I return the index of the maximum value of a list?

Discussion in 'Python' started by jj_frap, Jun 4, 2006.

  1. jj_frap

    jj_frap Guest

    I'm new to programming in Python and am currently writing a three-card
    poker simulator. I have completed the entire simulator other than
    determining who has the best hand (which will be far more difficult
    than the aspects I've codes thus far)...I store each player's hand in a
    list of hand objects and I determine hand strength via a handstrength
    list with one element for each player.

    When I try to print the "winner" (I've not coded for kicker strength
    and ties yet) via the max function, it returns the maximum value in the
    list rather than the index associated with that value.

    How do I return the index?
     
    jj_frap, Jun 4, 2006
    #1
    1. Advertising

  2. jj_frap enlightened us with:
    > When I try to print the "winner" (I've not coded for kicker strength
    > and ties yet) via the max function, it returns the maximum value in
    > the list rather than the index associated with that value.
    >
    > How do I return the index?


    You can't even be sure it exists - there might be multiple maximum
    values. What would you expect in such a case?

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Jun 4, 2006
    #2
    1. Advertising

  3. jj_frap

    gene tani Guest

    jj_frap wrote:
    > I'm new to programming in Python and am currently writing a three-card
    > poker simulator. I have completed the entire simulator other than
    > determining who has the best hand (which will be far more difficult
    > than the aspects I've codes thus far)...I store each player's hand in a
    > list of hand objects and I determine hand strength via a handstrength
    > list with one element for each player.
    >
    > When I try to print the "winner" (I've not coded for kicker strength
    > and ties yet) via the max function, it returns the maximum value in the
    > list rather than the index associated with that value.
    >
    > How do I return the index?


    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/306862
    http://www.rubyquiz.com/quiz24.html
     
    gene tani, Jun 4, 2006
    #3
  4. Re: Max function question: How do I return the index of the maximumvalue of a list?

    gene tani wrote:

    > http://www.rubyquiz.com/quiz24.html


    His question was for three-card poker, not normal poker. The ranking of
    hands in three-card poker isn't the same as in normal best five-card
    poker rankings; for instance, in three-card poker, a straight beats a flush.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    Could it be / That we need loving to survive
    -- Neneh Cherry
     
    Erik Max Francis, Jun 4, 2006
    #4
  5. Re: Max function question: How do I return the index of the maximumvalue of a list?

    jj_frap wrote:
    > I'm new to programming in Python and am currently writing a three-card
    > poker simulator. I have completed the entire simulator other than
    > determining who has the best hand (which will be far more difficult
    > than the aspects I've codes thus far)...I store each player's hand in a
    > list of hand objects and I determine hand strength via a handstrength
    > list with one element for each player.
    >
    > When I try to print the "winner" (I've not coded for kicker strength
    > and ties yet) via the max function, it returns the maximum value in the
    > list rather than the index associated with that value.
    >
    > How do I return the index?


    Can you do something like::

    max_val, max_index = max((x, i) for i, x in enumerate(my_list))

    ? If any two "x" values are equal, this will return the one with the
    lower index. Don't know if that matters to you.

    STeVe
     
    Steven Bethard, Jun 5, 2006
    #5
  6. jj_frap

    Robert Kern Guest

    Re: Max function question: How do I return the index of the maximumvalue of a list?

    Steven Bethard wrote:
    > Can you do something like::
    >
    > max_val, max_index = max((x, i) for i, x in enumerate(my_list))
    >
    > ? If any two "x" values are equal, this will return the one with the
    > lower index. Don't know if that matters to you.


    Wouldn't it return the one with the highest index?

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco
     
    Robert Kern, Jun 5, 2006
    #6
  7. Re: Max function question: How do I return the index of the maximumvalue of a list?

    Robert Kern wrote:
    > Steven Bethard wrote:
    >> Can you do something like::
    >>
    >> max_val, max_index = max((x, i) for i, x in enumerate(my_list))
    >>
    >> ? If any two "x" values are equal, this will return the one with the
    >> lower index. Don't know if that matters to you.

    >
    > Wouldn't it return the one with the highest index?


    Yes sorry. Mentally switched my min and max calls.

    Thanks for the catch.

    Steve
     
    Steven Bethard, Jun 5, 2006
    #7
    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. Hexamorph

    Re: Index of maximum element in list

    Hexamorph, Jan 25, 2008, in forum: Python
    Replies:
    15
    Views:
    574
    Arnaud Delobelle
    Jan 27, 2008
  2. W. eWatson
    Replies:
    8
    Views:
    220
  3. ngoc
    Replies:
    5
    Views:
    179
    Tad McClellan
    May 11, 2006
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    306
    Tomasz Chmielewski
    Mar 4, 2008
  5. phanhuyich
    Replies:
    4
    Views:
    278
Loading...

Share This Page