计算列表python中的列表数

count number of lists inside a list python

本问题已经有最佳答案,请猛点这里访问。

我有一个这样的列表:

1
2
3
4
test=[[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1],
    [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1],
    [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1],
    [1, 0, 0, 0], [1, 0, 0, 0],[0,0,0,0]]

我如何计算我在名为test的列表中有多少个列表?


您将为此任务使用len功能:

Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set).

1
2
3
4
5
6
>>> test=[[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1],
...     [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1],
...     [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1],
...     [1, 0, 0, 0], [1, 0, 0, 0],[0,0,0,0]]
>>> len(test)
18