Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Max function question: How do I return the index of the maximum value of a list?

Reply
Thread Tools

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

 
 
jj_frap
Guest
Posts: n/a
 
      06-04-2006
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?

 
Reply With Quote
 
 
 
 
Sybren Stuvel
Guest
Posts: n/a
 
      06-04-2006
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
 
Reply With Quote
 
 
 
 
gene tani
Guest
Posts: n/a
 
      06-04-2006

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/Coo.../Recipe/306862
http://www.rubyquiz.com/quiz24.html

 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      06-04-2006
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.velocityreviews.com/forums/(E-Mail Removed) && 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
 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      06-05-2006
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
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      06-05-2006
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

 
Reply With Quote
 
Steven Bethard
Guest
Posts: n/a
 
      06-05-2006
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
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Is there a simple way to find the list index to the max value? W. eWatson Python 8 02-17-2010 04:44 AM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
The number name 'System.Web.UI.WebControls' contains more than the maximum number of prefixes. The maximum is 3. mayur ASP .Net 2 07-02-2004 10:35 AM



Advertisments