473,378 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 2490
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_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
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_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

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

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

Similar topics

9
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...
9
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...
4
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...
2
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**...
3
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...
9
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...
80
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...
36
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...
1
by: thesinik | last post by:
import random import math t = tuple() def linsearch(key, l): count = 0 index = 0 t = tuple() for i in l:
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.