简体   繁体   中英

How to find max() in a list and find its index value as well using max()?

Just wondering if someone could spot what i was doing wrong, i was under the impression that the following code would find the max number and the index of the max number. here is the code im using below.

def select_winner():
    print('The game scores are:')
    for i in range (4):
        print(Players[i], ' = ', score[i])
    winner = max(enumerate(score))
    print('----------------')
    print()
    print('The Winner is ', Players[winner[0]], ' with ', str(winner[1]),' 
    points!')
    print(winner)

The output:

#The game scores are:
#Jarrah  =  86
#Reza  =  121
#Marge  =  72
#Homer  =  91
#----------------
#The Winner is  Homer  with  91  points!
#(3, 91)

max should choose the highest value right? and enumerate should choose the value and its index if im not mistaken and when i print them together i should get the highest value and wheres its located in the list. well atleast thats what im trying to do. the index for the score that was chosen as the highest should share the same index with the player who got that score as well as thats how they were listed.

any help would be great thanks

Update:

def select_winner():
    print('The game scores are:')
    for i in range (4):
        print(Players[i], ' = ', score[i])
    winner =(max(score))
    print('----------------')
    print()
    print('The Winner is '+ Players[winner[0]]+ ' with '+ str(winner[1])+ ' 
    points!')
    print(winner)

output:

#The game scores are:
#Jarrah  =  91
#Baldwin  =  73
#Kepa  =  112
#Long  =  106
#----------------
#in select_winner
#print('The Winner is '+ Players[winner[0]]+ ' with '+ str(winner[1])+ ' 
#points!')
#TypeError: 'int' object is not subscriptable

anyone know of a work around, and does the max() on its own pull the index of where the max number is located? if not, is there a way to do that?

Fixed!:

def select_winner():
    k=0
    print('The game scores are:')
    for i in range (4):
    print(Players[i], ' = ', score[i])
    winner2=(score.index(max(score)))
    winner=str(max(score))
    print('----------------')
    print()
    print('The Winner is '+ Players[winner2]+ ' with '+ winner + ' points!')

You want to use max(score) ; enumerate returns a tuple (index, element) ; the max of a tuple is evaluated on the first element, which will always be the last (largest index) in this case.

winner = max(score)

If you also want the indices, you can do as was suggested by @ChrisRand in the comments:

winner = max(enumerate(score), key= lambda x: x[1])

Enumerate gives you tuples of index and item. For example if

>>> score
[1, 2, 4, 9, 6, 8, 3, 7, 4, 8]
>>> [i for i in enumerate(score)]
[(0, 1), (1, 2), (2, 4), (3, 9), (4, 6), (5, 8), (6, 3), (7, 7), (8, 4), (9, 8)]

Just a simple max(score) would work for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM