30秒学会 Python 片段 · 2023年4月7日

30秒学会 Python 片段 – Key of min value

Finds the key of the minimum value in a dictionary.

  • Use min() with the key parameter set to dict.get() to find and return the key of the minimum value in the given dictionary.

代码实现

def key_of_min(d):
  return min(d, key = d.get)

使用样例

key_of_min({'a':4, 'b':0, 'c':13}) # b

翻译自:https://www.30secondsofcode.org/python/s/key-of-min