Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.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)_Python_Arrays_List_Recursion - Fatal编程技术网

如何查找输入中的列表数?(python)

如何查找输入中的列表数?(python),python,arrays,list,recursion,Python,Arrays,List,Recursion,有人知道怎么做吗? 我所拥有的只是 def lists(A: list) -> int: '''Return the total number of lists in A (including A itself). Each element of A and any nested lists are either ints or other lists. Example: >>> lists([1, 2, 3]) 1

有人知道怎么做吗? 我所拥有的只是

def lists(A: list) -> int:

    '''Return the total number of lists in A (including A itself).
    Each element of A and any  nested lists are either ints or other lists.

    Example:
    >>> lists([1, 2, 3])
    1
    >>> lists([[1], [2], [3]])
    4
    >>> lists([[[1, 2], [], 3]])
    4
    '''

这里有一种写的方法:

for i in range(0, len(A)):
    if (isinstance(A[i], list)):
        count=count+1
        return(lists(A[i]))
    else:
        B=A[i:]
return(count)
样本输出:

def numlists(lst, num = 1):
    for item in lst:
        if isinstance(item, list):
            num += numlists(item)
    return num

这里有一种写的方法:

for i in range(0, len(A)):
    if (isinstance(A[i], list)):
        count=count+1
        return(lists(A[i]))
    else:
        B=A[i:]
return(count)
样本输出:

def numlists(lst, num = 1):
    for item in lst:
        if isinstance(item, list):
            num += numlists(item)
    return num

您应该使用递归执行此操作:

print(numlists([1, 2, 3])) # 1
print(numlists([[1], [2], [3]])) # 4
print(numlists([[[1, 2], [], 3]])) # 4
print(numlists([[1,[2,3,[4,5,[6]]]],7,8,[9]])) # 6

您应该使用递归执行此操作:

print(numlists([1, 2, 3])) # 1
print(numlists([[1], [2], [3]])) # 4
print(numlists([[[1, 2], [], 3]])) # 4
print(numlists([[1,[2,3,[4,5,[6]]]],7,8,[9]])) # 6

这里有一个“肮脏”但简单的方法

def count_list(a):
    result = 0
    if isinstance(a, list):
        result += 1
    try:
        for b in a:
            result += count_list(b)
    except:
        pass
    return result

不需要递归

这里有一个“脏”但简单的方法

def count_list(a):
    result = 0
    if isinstance(a, list):
        result += 1
    try:
        for b in a:
            result += count_list(b)
    except:
        pass
    return result
def lists(l):
    '''
    Return the total number of lists in A (including A itself).
    Each element of A and any  nested lists are either ints or other lists.
    '''

    # convert the list to string and count the ['s
    # each [ is the start of a list, so the number of ['s equals
    # the number of lists
    nr_of_lists = str(l).count('[')

    # return the number of sublists
    return nr_of_lists

不需要递归

为什么要创建
B
?不需要这样做。既然您试图用递归来解决这个问题,那么您的基本情况是什么?什么是递归步骤?return(list(A[i])是我的递归步骤,当没有更多的元素需要检查时,递归应该停止为什么要创建
B
?您不需要这样做。既然您试图用递归来解决这个问题,那么您的基本情况是什么?你的递归步骤是什么?return(list(A[i])是我的递归步骤,当没有更多的元素需要检查时,递归应该停止。如果列表包含包含“[”?@timgeb的字符串,docstring声明它只包含子列表或intsOh,对,无需担心。尽管如此,还是相当脏;)如果列表包含包含['?@timgeb文档字符串声明它只包含子列表或intsOh,对吧,那么就不管了。尽管如此,还是相当脏;)
def lists(l):
    '''
    Return the total number of lists in A (including A itself).
    Each element of A and any  nested lists are either ints or other lists.
    '''

    # convert the list to string and count the ['s
    # each [ is the start of a list, so the number of ['s equals
    # the number of lists
    nr_of_lists = str(l).count('[')

    # return the number of sublists
    return nr_of_lists