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

J

jj_frap

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?
 
S

Sybren Stuvel

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
 
G

gene tani

jj_frap said:
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
 
S

Steven Bethard

jj_frap said:
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
 
R

Robert Kern

Steven said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top