简体   繁体   English

如何在列表中找到max()并使用max()查找其索引值?

[英]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? 任何人都知道解决方法,max()本身是否会拉出最大编号所在的索引? 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) ; 您想使用max(score) ; enumerate returns a tuple (index, element) ; enumerate返回一个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: 如果还需要索引,则可以按照@ChrisRand在注释中建议的方法进行操作:

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. 只需一个简单的max(score)为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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