【问题标题】:Counting elements in a list of lists, and returning the count of each list as a list计算列表列表中的元素,并将每个列表的计数作为列表返回
【发布时间】:2021-07-03 15:51:19
【问题描述】:

我想计算列表列表的元素,然后将输出作为每个列表的计数列表。这是我制作的列表示例。

ListofLists = [[5, 3, 5, 4], ["a", "b", "c"]]

然后我想计算每个列表的元素并将其作为列表返回:

CountofLists = [4, 3]

任何帮助将不胜感激

【问题讨论】:

    标签: python-3.x list count


    【解决方案1】:

    您可以组合map()len() 内置函数:

    CountofLists = [*map(len, ListofLists)]
    print(CountofLists)
    

    打印:

    [4, 3]
    

    或者:

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

    【讨论】:

      最近更新 更多