在python列表中找到最长的列表

编程入门 行业动态 更新时间:2024-10-27 12:32:30
本文介绍了在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列表中找到最长的列表

本文发布于:2023-11-03 07:38:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554593.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最长   列表   列表中   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!