如何在列表中查找嵌套列表的数量?

时间:2021-08-10 18:22:06

The function takes a list and returns an int depending on how many lists are in the list not including the list itself. (For the sake of simplicity we can assume everything is either an integer or a list.)

该函数采用一个列表并返回一个int,具体取决于列表中有多少列表不包括列表本身。 (为简单起见,我们可以假设所有内容都是整数或列表。)

For example:

例如:

x=[1,2,[[[]]],[[]],3,4,[1,2,3,4,[[]] ] ]

count_list(x) # would return 8

I think using recursion would help but I am not sure how to implement it, this is what I have so far.

我认为使用递归会有所帮助,但我不知道如何实现它,这是我到目前为止所做的。

def count_list(a,count=None, i=None):

    if count==None and i==None:
        count=0
        i=0
    if i>len(a)
        return(count)
    if a[i]==list
       i+=1
       count+=1
       return(count_list(a[i][i],count))
    else:
        i+=1
        return(count_list(a[i]))

5 个解决方案

#1


17  

This seems to do the job:

这似乎做了这个工作:

def count_list(l):
    count = 0
    for e in l:
        if isinstance(e, list):
            count = count + 1 + count_list(e)
    return count

#2


21  

You can do it with a recursion function :

您可以使用递归函数执行此操作:

def count(l):
    return sum(1+count(i) for i in l if isinstance(i,list))

Demo:

演示:

>>> x=[1,2,[[[]]],[[]],3,4,[1,2,3,4,[[]] ] ]
>>> count(x)
8

#3


5  

Here is a non-recursive solution:

这是一个非递归的解决方案:

  1. First, put every items of the list into a stack
  2. 首先,将列表中的每个项目放入堆栈
  3. Keep popping an item off the stack until it is exhausted
  4. 继续从堆栈中弹出一个项目,直到它耗尽为止
  5. If the item is a list: a) count it, b) push every items in in to the stack
  6. 如果该项目是一个列表:a)计算它,b)将每个项目推入堆栈

The code:

代码:

def count_list(lst):
    """ Given a master list, count the number of sub-lists """
    stack = lst[:]
    count = 0
    while stack:
        item = stack.pop()
        if isinstance(item, list):
            # If the item is a list, count it, and push back into the
            # stack so we can process it later
            count += 1
            stack.extend(item)
    return count

#4


3  

A functional-style solution without loops. Recursively processes the first element of a list, and the tail of a list. Add one for each empty-list encountered (that is, once we finish processing some list, its tail becomes empty, and we add 1 to the result). And subtract 1 for the list itself.

没有循环的功能型解决方案。递归处理列表的第一个元素和列表的尾部。为遇到的每个空列表添加一个(也就是说,一旦我们完成处理某个列表,它的尾部变为空,我们将1添加到结果中)。并为列表本身减去1。

def number_of_lists(x):
    f = lambda x: 0 if not isinstance(x,list) else (f(x[0]) + f(x[1:]) if len(x) else 1)
    return f(x) - 1

Results:

结果:

x=[1,2,[[[]]],[[]],3,4,[1,2,3,4,[[]] ] ]
number_of_lists(x)
>> 8

#5


3  

I like this tail recursive solution, although it's not much use in Python...

我喜欢这种尾递归解决方案,虽然它在Python中用得不多......

def count_lists(l, counter):
    if (len(l) == 0):
        return counter
    else:
        e = l.pop(0)
        if (isinstance(e, list)):
            l.extend(e)
            return count_lists(l, 1 + counter)
        else:
            return count_lists(l, counter)

x=[1,2,[[[]]],[[]],3,4,[1,2,3,4,[[]]]]
print(count_lists(x, 0))

#1


17  

This seems to do the job:

这似乎做了这个工作:

def count_list(l):
    count = 0
    for e in l:
        if isinstance(e, list):
            count = count + 1 + count_list(e)
    return count

#2


21  

You can do it with a recursion function :

您可以使用递归函数执行此操作:

def count(l):
    return sum(1+count(i) for i in l if isinstance(i,list))

Demo:

演示:

>>> x=[1,2,[[[]]],[[]],3,4,[1,2,3,4,[[]] ] ]
>>> count(x)
8

#3


5  

Here is a non-recursive solution:

这是一个非递归的解决方案:

  1. First, put every items of the list into a stack
  2. 首先,将列表中的每个项目放入堆栈
  3. Keep popping an item off the stack until it is exhausted
  4. 继续从堆栈中弹出一个项目,直到它耗尽为止
  5. If the item is a list: a) count it, b) push every items in in to the stack
  6. 如果该项目是一个列表:a)计算它,b)将每个项目推入堆栈

The code:

代码:

def count_list(lst):
    """ Given a master list, count the number of sub-lists """
    stack = lst[:]
    count = 0
    while stack:
        item = stack.pop()
        if isinstance(item, list):
            # If the item is a list, count it, and push back into the
            # stack so we can process it later
            count += 1
            stack.extend(item)
    return count

#4


3  

A functional-style solution without loops. Recursively processes the first element of a list, and the tail of a list. Add one for each empty-list encountered (that is, once we finish processing some list, its tail becomes empty, and we add 1 to the result). And subtract 1 for the list itself.

没有循环的功能型解决方案。递归处理列表的第一个元素和列表的尾部。为遇到的每个空列表添加一个(也就是说,一旦我们完成处理某个列表,它的尾部变为空,我们将1添加到结果中)。并为列表本身减去1。

def number_of_lists(x):
    f = lambda x: 0 if not isinstance(x,list) else (f(x[0]) + f(x[1:]) if len(x) else 1)
    return f(x) - 1

Results:

结果:

x=[1,2,[[[]]],[[]],3,4,[1,2,3,4,[[]] ] ]
number_of_lists(x)
>> 8

#5


3  

I like this tail recursive solution, although it's not much use in Python...

我喜欢这种尾递归解决方案,虽然它在Python中用得不多......

def count_lists(l, counter):
    if (len(l) == 0):
        return counter
    else:
        e = l.pop(0)
        if (isinstance(e, list)):
            l.extend(e)
            return count_lists(l, 1 + counter)
        else:
            return count_lists(l, counter)

x=[1,2,[[[]]],[[]],3,4,[1,2,3,4,[[]]]]
print(count_lists(x, 0))