在Python列表中查找最长和最短的列表 [英] Finding the longest and the shortest lists within a list in Python

查看:81
本文介绍了在Python列表中查找最长和最短的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要打印列表中具有最小和最大项目数的列表.

I need to print the lists which have minimum and maximum number of items in a list.

例如,如果我有:

total_list = [[1, 2, 3], [1, 2, 3, 4], [1, 2, 3, 4, 5]]

我需要返回具有最小和最大长度的列表.我该怎么办?

I need to return the lists with a minimum and maximum lengths. How can I do it?

输出可能类似于:

total_list[0] and total_list[2]

python中的

推荐答案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆