如何检索字典及其对应键的最大值?你知道吗
my_dict = {1: 4, 2: 5, 3: 2, 4: 5, 5: 3}
maxValue = []
maximum = 0
for key, value in my_dict.items():
if value >= maximum:
maxValue.append(value)
number = max(maxValue)
print(number)
我只能得到最大值,结果是5,这是预期的,但我似乎也检索不到最大值对应的键。在这种情况下,对应的键应该是2和4。你知道吗
Tags:
您可以使用
operator.itemgetter
来实现:输出
您可以构建由
my_dict
中的值索引的键列表的dict,以便将max值映射到对应的键列表:这将输出:
你可以试试这个
这将输出为:
如果你只想找回钥匙那么你可以-
这将给出-
希望这有帮助!你知道吗
相关问题 更多 >
编程相关推荐