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

30秒学会 Python 片段 – Longest item

Takes any number of iterable objects or objects with a length property and returns the longest one.

  • Use max() with len() as the key to return the item with the greatest length.
  • If multiple items have the same length, the first one will be returned.

代码实现

def longest_item(*args):
  return max(args, key = len)

使用样例

longest_item('this', 'is', 'a', 'testcase') # 'testcase'
longest_item([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]) # [1, 2, 3, 4, 5]
longest_item([1, 2, 3], 'foobar') # 'foobar'

翻译自:https://www.30secondsofcode.org/python/s/longest-item