计算列表内列表中的最小长度 [英] Calculating minimum length among the lists inside a list

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

问题描述

a=[[1,0,1,2,1,1,1,3111111],[31,1,4,51,1,1,1],[1,1,6,7,8]]
print min(a[0],a[1],a[2])

以下代码返回[1, 0, 1, 2, 1, 1, 1, 3111111].不确定默认密钥是什么,并根据逻辑返回什么?

The following code returns the [1, 0, 1, 2, 1, 1, 1, 3111111]. Not sure what is the default key and according to what logic is it returned?

另外,我实际上是在尝试从列表中的这些列表中查找最小长度.

Plus I was actually trying to find the minimum length out of these lists within a list.

我写了这个min(len(a[0]),len(a[1]),len(a[2])).可以做得更好吗?

I wrote this min(len(a[0]),len(a[1]),len(a[2])). Can this be made any better?

推荐答案

是的,您可以使用map遍历内部列表以创建长度列表,然后使用min获得最小值:

Yes, you can use map to iterate over the inner lists to create a list of lengths, then get the minimum with min:

>>> a=[[1,0,1,2,1,1,1,3111111],[31,1,4,51,1,1,1],[1,1,6,7,8]]
>>> min(map(len, a))
 5

这篇关于计算列表内列表中的最小长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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