【发布时间】:2015-12-06 08:05:49
【问题描述】:
假设我有一个字典,其中包含不同键的多个最大值。我尝试使用代码:
taste = {"Mussels": 4, "Limpets": 4, "Prawn": 2, "Plankton":1}
print(max(taste, key=taste.get))
但它只给我贻贝或帽贝,取决于哪个先来。我尝试设置最高值,然后遍历我的键和每个键,我的值,例如:
highest = max(taste.values())
for i in taste.keys():
for j in taste[i]:
if j == highest:
print(i)
但这似乎不起作用,因为您不能像我的字典中的值那样通过整数进行交互。那么最干净,最简单的方法是什么
【问题讨论】:
-
您到底想要什么作为输出
[["Mussels"], [Limpets]]或任意(随机)顺序中的任何一个? -
你只想要
if taste[i] == highest,确定吗?
标签: python python-3.x dictionary printing max