# 获取字典中对应于最小值的键

` `{320:1, 321:0, 322:3}` `

### 12 Solutions collect form web for “获取字典中对应于最小值的键”

` `>>> 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)` `

`min(d.items(), key=lambda x: x[1])[0]`

` `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']` `
` `>>> d = {320:1, 321:0, 322:3} >>> min(d, key=lambda k: d[k]) 321` `

` `d = {320:1, 321:0, 322:3, 323:0} print ', '.join(str(key) for min_value in (min(d.values()),) for key in d if d[key]==min_value) """Output: 321, 323 """` `

` `>>> dd = {320:1, 321:0, 322:3, 323:0} >>> >>> from itertools import groupby >>> from operator import itemgetter >>> >>> print [v for k,v in groupby(sorted((v,k) for k,v in dd.iteritems()), key=itemgetter(0)).next()[1]] [321, 323]` `

` `from operator import itemgetter min_key, _ = min(d.iteritems(), key=itemgetter(1))` `
` `d={} d[320]=1 d[321]=0 d[322]=3 value = min(d.values()) for k in d.keys(): if d[k] == value: print k,d[k]` `
` `# python d={320:1, 321:0, 322:3} reduce(lambda x,y: x if d[x]<=d[y] else y, d.iterkeys()) 321` `

` `d = dict() d[15.0]='fifteen' d[14.0]='fourteen' d[14.5]='fourteenandhalf' print d[min(d.keys())]` `