关于字典访问的问题……

img


怎么输入最高分对应的学号呐?我试过将字典转化为列表和元组,都不可以……

你要是没学过列表推导式,就循环一下

max=0
for key,value in dictScore.items():
    if max<value:
        max=value

for key,value in dictScore.items():
    if value==max:
        print(key)

你试试下面的代码是不是符合要求
1、代码:max(dictScore,key=lambda x : dictScore[x])
2、解析:max函数是找最大值,上面代码的意思就是查找dictScore中分数(value)的最大值


dictScore = {
    '101': 45,
    '102': 85,
    '103': 95,
    '104': 96,
    '105': 78,
    '106': 96
}
print('平均分数为:{:.1f}'.format(sum(dictScore.values())/len(dictScore)))
for key, value in dictScore.items():
    if value == max(dictScore.values()):
        print('最高分对应的学号为:{}'.format(key))