473,573 Members | 3,524 Online

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

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?

Jun 4 '06 #1
6 2505
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
Jun 4 '06 #2

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

Jun 4 '06 #3
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 && ma*@alcyone.com && 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
Jun 4 '06 #4
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_li st))

? 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
Jun 5 '06 #5
Steven Bethard wrote:
Can you do something like::

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

? 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
an underlying truth."
-- Umberto Eco

Jun 5 '06 #6
Robert Kern wrote:
Steven Bethard wrote:
Can you do something like::

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

? 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
Jun 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 9 4942 by: Penn Markham | last post by: Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my webserver runs that part of the script (see attached file, snippet.php), though, it doesn't go through. I don't get an error message or... 9 2015 by: me | last post by: Hi All, I am new to Classes and learniing the ropes with VB.NET express Here's my question - say I have a want to manage a list of books. Each book has an Author, Title and ISBN Now, I am used to using Arrays so I would normally do something like this: Set an array up during the init routine (called from form_load) say of 4 1953 by: comp.lang.tcl | last post by: I wrote this PHP function in the hopes that it would properly use a TCL proc I wrote about 4 years ago: if (!function_exists('proper_case')) { /** * Ths function will convert a string into a proper case format using the customized TCL proc "PROPER_CASE" from the included TCL string tools libraries * * @access public 2 5010 by: king imran | last post by: I am giving u complete code. It works properly But add max and mean functions so that it can work properly. * /LinkList.h/ * #include "Node.h" * /* The LinkList class declaration*/ class** LinkList { 3 2944 by: Michel Esber | last post by: Hi all, Db2 v8 FP15 LUW . create table T (ID varchar (24), ABC timestamp) There is an index for (ID, ABC), allowing reverse Scans. My application needs to determine MIN and MAX(ABC) for a given ID. We are currently using a simple statement: 9 2621 by: pereges | last post by: Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object,... 80 2391 by: Boltar | last post by: Hi I need to store a number of integer values which I will then search on later to see if they exist in my container. Can someone tell me which container would be quickest for finding these values? I can't use a plain C array (unless I make it 2^32 in size!) since I don't know the max integer value. Thanks for any help 36 4162 by: AB3004 | last post by: Hi, I'm currently working on this piece of code & unfortunately, whenever I run it, it states that selectNumbers isn't defined and it errors on Line 1 (?) can anyone tell me why? I'm tearing my hair out! M150 TMA 5 : Programming : Task 3 - User selection of balls