Python 比较字典中列表之间的整数

Python 比较字典中列表之间的整数,python,Python,我是Python新手,我正在尝试为游戏制作一个计算器。我想在列表(字典中的值)和最大值来自的键之间的特定索引处获得最大整数 我试着把字典翻了一遍 raw_player_score = {'Matt' : [3, 5, 5, 4, 6, 9], 'Kyle' : [6, 9, 11, 5, 4, 3], 'Emily' : [4, 4, 5, 2, 1, 5]} def extra_points(dict):

我是Python新手,我正在尝试为游戏制作一个计算器。我想在列表(字典中的值)和最大值来自的键之间的特定索引处获得最大整数

我试着把字典翻了一遍

raw_player_score = {'Matt' : [3, 5, 5, 4, 6, 9],
                    'Kyle' : [6, 9, 11, 5, 4, 3],
                    'Emily' : [4, 4, 5, 2, 1, 5]}

def extra_points(dict):
    for k, v in dict.items():
        for number in v:
            apple_king = max(v[1])
            print(apple_king)

final_dict = extra_points(raw_player_score)

我希望结果是9,因为Kevin在索引1处的数字最高,但相反,我得到的信息是“'int'对象不可迭代”

硬编码所需的索引不是一个好主意;我建议您将其移动到一个参数。其他更改注释如下:

def extra_points(dict_, index):
    return max(                                    # Return maximum
        (                                          # From iterator
            (name, score_list[index])              # For name-score[index] tuples
            for name, score_list in dict_.items()  # In dict_
        ),
        key=lambda x: x[1]                         # Check the max by score[index]
    )[0]                                           # Get the name (zero element)

raw_player_score = {
    'Matt': [3, 5, 5, 4, 6, 9],
    'Kyle': [6, 9, 11, 5, 4, 3,],
    'Emily': [4, 4, 5, 2, 1, 5]
}

print(extra_points(raw_player_score, 1))
Kyle


硬编码所需的索引不是一个好主意;我建议您将其移动到参数中。其他更改注释如下:

def extra_points(dict_, index):
    return max(                                    # Return maximum
        (                                          # From iterator
            (name, score_list[index])              # For name-score[index] tuples
            for name, score_list in dict_.items()  # In dict_
        ),
        key=lambda x: x[1]                         # Check the max by score[index]
    )[0]                                           # Get the name (zero element)

raw_player_score = {
    'Matt': [3, 5, 5, 4, 6, 9],
    'Kyle': [6, 9, 11, 5, 4, 3,],
    'Emily': [4, 4, 5, 2, 1, 5]
}

print(extra_points(raw_player_score, 1))
Kyle


尽量不要将dict用作变量名,您可以尝试:

raw_player_score = {'Matt': [3, 5, 5, 4, 6, 9], 'Kyle': [6, 9, 11, 5, 4, 3], 'Emily': [4, 4, 5, 2, 1, 5]}


def extra_points(d, ind):
    values_at_index = []
    for key in d:
        values_at_index.append((key, d[key][ind]))
    return max(values_at_index, key=lambda x: x[1])


print(extra_points(raw_player_score, 1))
print(extra_points(raw_player_score, 1)[0])
其中:

('Kyle', 9)
Kyle

尽量不要将dict用作变量名,您可以尝试:

raw_player_score = {'Matt': [3, 5, 5, 4, 6, 9], 'Kyle': [6, 9, 11, 5, 4, 3], 'Emily': [4, 4, 5, 2, 1, 5]}


def extra_points(d, ind):
    values_at_index = []
    for key in d:
        values_at_index.append((key, d[key][ind]))
    return max(values_at_index, key=lambda x: x[1])


print(extra_points(raw_player_score, 1))
print(extra_points(raw_player_score, 1)[0])
其中:

('Kyle', 9)
Kyle

其他答案中的所有建议都是正确的。我将提供一个更简单、老派的解决方案,它只需做最少的工作,而不必创建任何额外的列表或进行任何排序。我认为作为一名新的Python程序员,最直接、最透明的方法可能会为您提供最好的服务:

raw_player_scores = {'Matt' : [3, 5, 5, 4, 6, 9],
                    'Kyle' : [6, 9, 11, 5, 4, 3,],
                    'Emily' : [4, 4, 5, 2, 1, 5]}

def extra_points(scores, pos):
    max_score = 0
    max_key = None
    for k, v in scores.items():
        if v[pos] > max_score:
            max_score = v[pos]
            max_key = k
    return max_key

max_key = extra_points(raw_player_scores, 1)
print(max_key)
结果:

Kyle

其他答案中的所有建议都是正确的。我将提供一个更简单、老派的解决方案,它只需做最少的工作,而不必创建任何额外的列表或进行任何排序。我认为作为一名新的Python程序员,最直接、最透明的方法可能会为您提供最好的服务:

raw_player_scores = {'Matt' : [3, 5, 5, 4, 6, 9],
                    'Kyle' : [6, 9, 11, 5, 4, 3,],
                    'Emily' : [4, 4, 5, 2, 1, 5]}

def extra_points(scores, pos):
    max_score = 0
    max_key = None
    for k, v in scores.items():
        if v[pos] > max_score:
            max_score = v[pos]
            max_key = k
    return max_key

max_key = extra_points(raw_player_scores, 1)
print(max_key)
结果:

Kyle

Pseudocode:NameError-这不起作用。请修复您的“成为python”。凯文?谁是凯文?
max(v[1])
没有任何意义,因为
v[1]
是一个整数。在调用
max()
之前,您需要从字典中的列表中建立一个二级列表。其中
dx=1
code
max((vals[dx],name)表示原始玩家分数中的(name,vals)。items()
给出
(9,'Kyle')
。领带将按姓名的字母顺序倒序决定。伪代码:name错误-这不按原样工作。请修复您的python身份。凯文?谁是凯文?
max(v[1])
没有任何意义,因为
v[1]
是一个整数。在调用
max()
之前,需要从字典中的列表中建立一个二级列表。其中
dx=1
code
max((vals[dx],name)表示原始玩家分数中的(name,vals)。items())
给出
(9,'Kyle'))
.Ties将由名称的逆字母顺序决定。您的解决方案比仅将索引作为参数传递有更多的更改。
返回max(dict_,key=lambda name:dict_[name][index])
您的解决方案比仅将索引作为参数传递有更多的更改。
返回max(dict_,key=lambda name:dict_[name][索引]
def extra_points(d,ind):返回d.items()中(k,v)的最大值((v[ind],k))
@StevenRumbalski,cool one liner:)也许可以发布一个答案,让观众更容易看到。你正在制作一个中间列表,这个列表冗长、不必要,并且使你的代码可读性较差。另外,在索引中创建
值时不使用列表理解也会使你的代码变得不地道。重点是这不是一行代码。这是一行代码,因为这是问题的复杂性。同样,代码不必是一行代码,代码也不必有列表。我选择了这种方式。我希望你的日子过得更好。
def extra_points(d,ind):返回max((v[ind],k)for(k,v)in d.items())
@StevenRumbalski,cool one liner:)也许可以发布一个答案,让观众更容易看到。你正在制作一个中间列表,这个列表冗长、不必要,并且使你的代码可读性较差。另外,在索引中创建
值时不使用列表理解也会使你的代码变得不地道。重点是这不是一行代码。这是一行代码,因为这是问题的复杂性。同样,代码不必是一行代码,代码也不必没有列表。我选择了这种方式。我希望你的日子过得更好。