Python在子列表中查找列表长度 [英] Python find list lengths in a sublist
本文介绍了Python在子列表中查找列表长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试找出如何获取特定列表中包含的每个列表的长度.例如:
I am trying to find out how to get the length of every list that is held within a particular list. For example:
a = []
a.append([])
a[0].append([1,2,3,4,5])
a[0].append([1,2,3,4])
a[0].append([1,2,3])
我想运行以下命令:
len(a[0][:])
这将输出我想要的答案,该答案是长度[5,4,3]的列表.该命令显然无效,我尝试过的其他命令也无效.请帮忙!
which would output the answer I want which is a list of the lengths [5,4,3]. That command obviously does not work, and neither do a few others that I've tried. Please help!
推荐答案
[len(x) for x in a[0]]
吗?
>>> a = []
>>> a.append([])
>>> a[0].append([1,2,3,4,5])
>>> a[0].append([1,2,3,4])
>>> a[0].append([1,2,3])
>>> [len(x) for x in a[0]]
[5, 4, 3]
这篇关于Python在子列表中查找列表长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文