3 回答
TA贡献1869条经验 获得超4个赞
这实际上是提供OP所需解决方案的答案:
>>> d = {320:1, 321:0, 322:3}
>>> d.items()
[(320, 1), (321, 0), (322, 3)]
>>> # find the minimum by comparing the second element of each tuple
>>> min(d.items(), key=lambda x: x[1])
(321, 0)
d.iteritems()但是,对于较大的词典,使用将更为有效。
TA贡献1829条经验 获得超4个赞
对于您有多个最小键并希望保持简单的情况
def minimums(some_dict):
positions = [] # output variable
min_value = float("inf")
for k, v in some_dict.items():
if v == min_value:
positions.append(k)
if v < min_value:
min_value = v
positions = [] # output variable
positions.append(k)
return positions
minimums({'a':1, 'b':2, 'c':-1, 'd':0, 'e':-1})
['e', 'c']
添加回答
举报