Python错误,生成的键为max of mins

Python错误,生成的键为max of mins,python,dictionary,Python,Dictionary,我正在尝试编写一个函数,该函数使用一个字典,其中每个键都被分配给一个值列表。此函数用于执行一个游戏,在该游戏中,从每个关键点获取并比较最小值的最大值,以及与游戏获胜者相关的最小值的最大值。如果所有玩家都与他们的最小值打成平局,那么将比较他们的第二个最小值,最大值将生成获胜的关键点。解决方案必须涉及字典/类/循环,但不涉及集合或递归。递归可以用来中断循环 例如: 确定优胜者({'A':[1,1],'B':[2,2],'C':[1,1]})产生'B'(因为B的最低分2大于其他玩家的最低分1 确定优胜

我正在尝试编写一个函数,该函数使用一个字典,其中每个键都被分配给一个值列表。此函数用于执行一个游戏,在该游戏中,从每个关键点获取并比较最小值的最大值,以及与游戏获胜者相关的最小值的最大值。如果所有玩家都与他们的最小值打成平局,那么将比较他们的第二个最小值,最大值将生成获胜的关键点。解决方案必须涉及字典/类/循环,但不涉及集合或递归。递归可以用来中断循环

例如:

确定优胜者({'A':[1,1],'B':[2,2],'C':[1,1]})
产生
'B'
(因为B的最低分2大于其他玩家的最低分1

确定优胜者({'A':[1,2,3,4],'B':[2,3,4,1],'C':[1,3,4,5]})
产生
'C'
(所有玩家最初的最低分数是1,但C的下一个最低分数是3,而A和B的下一个最低分数是2)

确定优胜者({'A':[1,2,3,4],'B':[2,3,4,1],'C':[1,4,1,5]})
产生
“平局”
(所有玩家最初以最低分数1平局,但随后A和B以2平局,而C以1平局,因此不再考虑。然后A和B再次以3平局,最后以4平局,因此平局无法打破)

到目前为止,我写的内容产生了一个错误:

def determine_winner(results):
    a = []
    max_mins = 0
    for key in results:
        if min(results[key]) > max_mins:
            winner = key
            max_mins = min(results[key])
        if min(results[key]) == max_mins:
            results = results[key].remove(min(results[key]))
    return winner

看起来您正在修改
结果
,同时在其上循环:

results = results[key].remove(min(results[key]))
删除最后的
if
语句将修复错误

对于实际的程序,此版本首先对结果进行排序,然后针对每个记分员对结果进行循环:

def determine_winner(results):
    print results
    for key in results:
        results[key].sort()          # sort all the results
        length = len(results[key])

    for l in range(length):          # examine the scores in order
        max_score = 0
        next_results = {}
        for key in results:          # compare each scorer
            score = results[key][l]
            if score < max_score:    # ignore this scorer
                continue
            if score == max_score:   # multiple high scores
                winner = 'Tied'
            else:                    # new high score
                winner = key
                max_score = score
            # prepare the results for the next round
            next_results[key] = results[key]
        results = next_results       # get ready for the next round
    print winner

determine_winner({'A':[1,1], 'B':[2,2], 'C':[1,1]})
determine_winner({'A':[1,2,3,4], 'B':[2,3,4,1], 'C':[1,3,4,5]})
determine_winner({'A':[1,2,3,4], 'B':[2,3,4,1], 'C':[1,4,1,5]})

这是作业吗?请发回回溯。非常感谢。我明白你关于结果的意思以及错误所在
{'A': [1, 1], 'C': [1, 1], 'B': [2, 2]}
B
{'A': [1, 2, 3, 4], 'C': [1, 3, 4, 5], 'B': [2, 3, 4, 1]}
C
{'A': [1, 2, 3, 4], 'C': [1, 4, 1, 5], 'B': [2, 3, 4, 1]}
Tied