J_l*_*log 1 python list max min
我只是python的新手,我需要打印列表中包含最小和最大项目的列表。
例如,如果我有:
total_list = [[1, 2, 3], [1, 2, 3, 4], [1,2,3,4,5]]
Run Code Online (Sandbox Code Playgroud)
我需要返回一个列表,列表的最大长度为最小。我怎样才能简单地用python方式呢?
我尝试对其进行迭代,但是我能收到的只是len列表中每一项的唯一内容
输出必须是:
total_list[0] and total_list[2]
Run Code Online (Sandbox Code Playgroud)
提前致谢
max并且minpython中的函数接受一个key参数,该参数将根据定义为键的值找到可迭代的最大值。因此,请尝试以下操作:
max(total_list, key=len)
Run Code Online (Sandbox Code Playgroud)
然后您可以使用total_list.index这些索引