Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 对列表中的元素进行计数,并将每个列表的计数作为列表返回_Python 3.x_List_Count - Fatal编程技术网

Python 3.x 对列表中的元素进行计数,并将每个列表的计数作为列表返回

Python 3.x 对列表中的元素进行计数,并将每个列表的计数作为列表返回,python-3.x,list,count,Python 3.x,List,Count,我想对列表的元素进行计数,然后让输出成为每个列表的计数列表。这是我列出的一个例子 ListofLists = [[5, 3, 5, 4], ["a", "b", "c"]] 然后我想计算每个列表的元素,并将其作为列表返回,如下所示: CountofLists = [4, 3] 任何帮助都将不胜感激您可以将map()和len()内置函数组合在一起: CountofLists = [*map(len, ListofLists)] pri

我想对列表的元素进行计数,然后让输出成为每个列表的计数列表。这是我列出的一个例子

ListofLists = [[5, 3, 5, 4], ["a", "b", "c"]]
然后我想计算每个列表的元素,并将其作为列表返回,如下所示:

CountofLists = [4, 3]
任何帮助都将不胜感激

您可以将
map()
len()
内置函数组合在一起:

CountofLists = [*map(len, ListofLists)]
print(CountofLists)
印刷品:

[4, 3]

或:

CountofLists = [len(sublist) for sublist in ListofLists]
print(CountofLists)