我只是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列表中找到最长的列表
发布评论