Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-报告具有最高值的键,或tie_Python - Fatal编程技术网

python-报告具有最高值的键,或tie

python-报告具有最高值的键,或tie,python,Python,我知道这很简单,可能已经在某个地方得到了答案,但我在任何地方都很难找到它,可能是因为我在寻找错误的术语 我有这样的想法(但要复杂得多): 所以我最终得出了这些值: score[1] = 7 score[2] = 9 score[3] = 12 score[4] = 7 我想把一个变量设置为3,因为分数[3]是最大的 score[1] = 9 score[2] = 9 score[3] = 7 score[4] = 8 在本例中,它应该将变量设置为0或其他值,因为最大的数字是平局。如果必须使用

我知道这很简单,可能已经在某个地方得到了答案,但我在任何地方都很难找到它,可能是因为我在寻找错误的术语

我有这样的想法(但要复杂得多):

所以我最终得出了这些值:

score[1] = 7
score[2] = 9
score[3] = 12
score[4] = 7
我想把一个变量设置为3,因为分数[3]是最大的

score[1] = 9
score[2] = 9
score[3] = 7
score[4] = 8

在本例中,它应该将变量设置为0或其他值,因为最大的数字是平局。

如果必须使用字典来计算
分数,则可以使用函数形式:

def index_of_highest_score(scores):
    max_score = max(scores.values())
    keys = []
    for key, value in scores.iteritems():
        if value == max_score:
            keys.append(key)
    if len(keys) > 1:
        return 0
    else:
        return keys[0]

score = {}
score[1] = 7
score[2] = 9
score[3] = 12
score[4] = 7
print index_of_highest_score(score) # Prints 3

score[1] = 9
score[2] = 9
score[3] = 7
score[4] = 8
print index_of_highest_score(score) # Prints 0
这将生成得分最高的键的列表,无论是一个还是多个。

使用

如果
score.most_common()[0][1]==score.most_common()[1][1]
有两个相等的最大值,因此将变量设置为0

else将变量设置为score.most_common()[0][0]
哪个键的值最高

score=Counter()
score[1] = 9
score[2] = 9
score[3] = 7
score[4] = 8
print score
print score.most_common()[0][1],score.most_common()[1][1]
print score.most_common()[0][1]==score.most_common()[1][1]
Counter({1: 9, 2: 9, 4: 8, 3: 7})
9 9
True

你是想把分数变成一本字典({})还是一个列表([])?@CptSupermrkt这个作业在一个空的列表上是行不通的,OP也从来没有提到过列表。我知道,这就是为什么我试图弄清楚他想做什么,因为他的问题中有矛盾。我相信我想把分数变成一本字典,因为我在别处用while循环修改它,更改键。@CptSupermrkt什么矛盾?他们说“键”,使用字典语法,…字典没有索引。不知道为什么这被否决了。使用列表及其
.index
方法是这里最具吸引力的解决方案。起初我假设
score
是一个
list
。看到你的评论,然后检查问题并按下紧急按钮。谢谢你的提醒!我已经编辑了我的答案来处理这两种情况。这在技术上是正确的(而且非常优雅,提醒你),但是你不觉得OP从一开始就使用字典有点愚蠢吗?我们是不是应该推断他的键将不是花哨的列表索引?@AdamSmith我不判断,我只是假设OP有他们的原因。在这种情况下,我可能太天真了,因为示例使用递增整数。当OP的代码有意义时,我通常也不这样做,但是在这种情况下,我相信他是一个新手程序员,可以使用更多的方向。你的回答虽然正确,但没有提供多少信息。然而,你仍然获得了我的+1,因为你以最直接的方式解决了这个问题。请更仔细地阅读这个问题,并意识到这个问题与作者的意图最为接近。出色地使用
计数器
!谢谢,是的,唯一重要的值是前两个最高的值,所以最常见的值都会派上用场。这完全是对
计数器的非直观使用。你很幸运,这些值是整数!
max_score = max(score.values())
keys = [k for k in score if score[k] == max_score]
from collections import Counter

score=Counter()
score[1] = 7
score[2] = 9
score[3] = 12
score[4] = 7
print score
Counter({3: 12, 2: 9, 1: 7, 4: 7})
print score.most_common()[0][1],score.most_common()[1][1]
12 9
score=Counter()
score[1] = 9
score[2] = 9
score[3] = 7
score[4] = 8
print score
print score.most_common()[0][1],score.most_common()[1][1]
print score.most_common()[0][1]==score.most_common()[1][1]
Counter({1: 9, 2: 9, 4: 8, 3: 7})
9 9
True