簡體   English   中英

獲取列表列表列表的長度

[英]Getting the lengths of a list of lists of lists of lists

我有一些看起來有點奇怪的列表,如下所示:

listlist = [[], [[[[], []], [[]], []]], [[]]]

我試圖找到所有列表長度的總和。

如果它是一個簡單的多維數組(nxnxn x...),找到列表長度的總和將非常容易,但在這種情況下,我不確定應該從哪里開始。

我嘗試使用'for循環',但我認為這不是對這類問題的明確答案,應該有一個非常簡單的方法來解決這類問題。

我會很感激任何建議。 提前致謝! =]

這可能會將您推向正確的方向:

def lenall(lst):
    if isinstance(lst, list):
        return 1 + sum(map(lenall, lst))
    return 0

>>> lenall(listlist)
12

這實際上計算了數據結構中列表的總數。 如果要添加所有長度,則必須將其更改為:

def lenall(lst):
    if isinstance(lst, list):
        return len(lst) + sum(map(lenall, lst))
    return 0

>>> lenall(listlist)
11

這是一個可能的解決方案:

listlist = [[], [[[[], []], [[]], []]], [[]]]

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

print(length(listlist))

Output

11

這是應該做你想做的事情的代碼。

我包含了 function 的兩個版本,一個是為了清楚起見,另一個是更小但功能相同。

####################
# 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)

output 是:

12
12

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM