首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取列表列表的长度列表列表的列表长度

获取列表列表的长度列表列表的列表长度
EN

Stack Overflow用户
提问于 2020-04-28 14:48:48
回答 3查看 75关注 0票数 0

我有一个看起来有点奇怪的列表,如下所示:

代码语言:javascript
复制
listlist = [[], [[[[], []], [[]], []]], [[]]]

我试着找出所有列表长度的和。

如果它是一个简单的多维数组(n x ...),那么查找列表长度的总和将非常容易,但在这种情况下,我不确定应该从哪里开始。

我尝试使用'for loop',但我认为这不是解决这类问题的明确答案,应该有一种非常简单的方法来解决这类问题。

如果有任何建议,我将不胜感激。提前感谢!=]

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-04-28 15:01:09

这可能会将您推向正确的方向:

代码语言:javascript
复制
def lenall(lst):
    if isinstance(lst, list):
        return 1 + sum(map(lenall, lst))
    return 0

>>> lenall(listlist)
12

这实际上计算了数据结构中列表的总数。如果要将它们的所有长度相加,则必须将其更改为:

代码语言:javascript
复制
def lenall(lst):
    if isinstance(lst, list):
        return len(lst) + sum(map(lenall, lst))
    return 0

>>> lenall(listlist)
11
票数 0
EN

Stack Overflow用户

发布于 2020-04-28 15:02:12

这里有一个可能的解决方案:

代码语言:javascript
复制
listlist = [[], [[[[], []], [[]], []]], [[]]]

def length(lst):
    return len(lst) + sum(length(l) for l in lst if isinstance(l, list))

print(length(listlist))

输出

代码语言:javascript
复制
11
票数 0
EN

Stack Overflow用户

发布于 2020-04-28 15:11:55

下面的代码可以做你想做的事情。

我包含了该函数的两个版本,一个是为了清晰起见,另一个是较小但功能相同的版本。

代码语言:javascript
复制
####################
# Create a list of lists / elements
####################
#x = [[1,2,3],[3,4],[3,4,[3,4,5],[4],[5,4,6]],[4,3,4,5,[[[6]]]]]
x = [[], [[[[], []], [[]], []]], [[]]]

############################################################
# First implementation (included for clarity)
############################################################
def get_len_lists(this_list):
    # Set number of elements to 0
    num_elem = 0                              

    # Loop through each element in the list...
    for elem in this_list:                          

        # .. if it's a list...
        if type(elem) == list:                      

            # ... if the list is empty, count that as an element
            if elem == list():                      
                # ... so add one
                num_elem += 1                       
            else:
                # ... get the number of elements...
                num_elem += get_len_lists(elem) + 1 

        # ... otherwise...
        else:                                       
            # ... just add one to the length of the list
            num_elem += 1                           

    # Return the number of elements in the list
    return num_elem                                 


############################################################
# Smaller implementation
############################################################
def get_len_lists_2(this_list):
    # Set number of elements to 0
    num_elem = 0                                    

    # Loop through each element in the list...
    for elem in this_list:                          

        # We add one for each level, regardless of whether it is an element or a list
        num_elem += 1                              

        # If it's a list...
        if type(elem) == list:                      

            # .. get the number of elements in the list
            num_elem += get_len_lists(elem)         

    # Return the number of elements in the list
    return num_elem                                 


result1 = get_len_lists(x) + 1
result2 = get_len_lists_2(x) + 1

print(result1)
print(result2)

输出为:

代码语言:javascript
复制
12
12
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61474262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档