30秒学会 Python 片段 · 2023年3月5日

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

Finds the key of the maximum value in a dictionary.

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

代码实现

def key_of_max(d):
  return max(d, key = d.get)

使用样例

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

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