在python列表中找到最长的列表 [英] finding a longest list within the lists in python
问题描述
我只是python的新手,我需要打印列表中包含最小和最大项目的列表.
I'm just a new in python and I need to print the lists which has minimum and maximum items in a list.
例如,如果我有:
total_list = [[1, 2, 3], [1, 2, 3, 4], [1,2,3,4,5]]
我需要返回一个列表,列表的长度应为最小和最大.我该如何简单地以python方式做到这一点?
I need to return a list with a minimum and maximum length of itself. How can I do it simply in python way?
我尝试对其进行迭代,但是我只能收到列表中每个项目的唯一len
I try to iterate over it but all I could receive is the only len
of each item in a list
输出必须为:
total_list[0] and total_list[2]
预先感谢
推荐答案
max
和min
函数接受key
参数,该参数将根据定义为键的值找到可迭代的最大值.所以,试试这个:
max
and min
function in python accepts a key
argument that will find the max of an iterable based on what defined as a key. so, try this:
max(total_list, key=len)
然后您可以使用total_list.index
查找那些索引
then you can use total_list.index
to find those indexes
这篇关于在python列表中找到最长的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!