30秒学会 Python 片段 · 2022年4月28日

30秒学会 Python 片段 – N max elements

Returns the n maximum elements from the provided list.

  • Use sorted() to sort the list.
  • Use slice notation to get the specified number of elements.
  • Omit the second argument, n, to get a one-element list.
  • If n is greater than or equal to the provided list’s length, then return the original list (sorted in descending order).

代码实现

def max_n(lst, n = 1):
  return sorted(lst, reverse = True)[:n]

使用样例

max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3, 2]

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