寻找最高的钥匙 [英] Finding the highest key

查看:54
本文介绍了寻找最高的钥匙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是对为什么我的代码无法正常工作感到困惑,这是我迄今为止遇到的问题和代码(测试运行表明我的答案是错误的).

I'm just confused about why my code would not work, here's the question and the code I have so far (the test run says my answer is wrong).

给出字典d,在字典中找到最大的键,并将相应的值与变量val_of_max关联.例如,给定字典{5:3, 4:1, 12:2},则2将与val_of_max关联.假设d不为空.

Given the dictionary d, find the largest key in the dictionary and associate the corresponding value with the variable val_of_max. For example, given the dictionary {5:3, 4:1, 12:2}, 2 would be associated with val_of_max. Assume d is not empty.

d = {5:3, 4:1, 12:2, 14:9}
val_of_max = max(d.keys())
print val_of_max

推荐答案

您的代码将以最大值显示.您想要的是:

your code prints the key with the maximum value. What you want is:

d = {5:3, 4:1, 12:2, 14:9}
val_of_max = d[max(d.keys())]
print val_of_max

也就是说,您必须取消引用键才能返回值.

That is, you have to dereference the key to return the value.

这篇关于寻找最高的钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆