0

I have a little bit weird looking lists like this:

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

and I am trying to find the sum of all the lengths of the lists.

If it was a simple multidimensional array(n x n x n x ...), finding the total sum of the lists' lengths would be very easy, but in this case, I am not sure where I should start.

I tried to use 'for loop', but I believe that is not a clear answer to this kind of problem, and there should be a really simple approach to solve this kind of problem.

I would appreciate any advice. Thanks in advance! =]

Kevin Choi
  • 747
  • 1
  • 5
  • 17

3 Answers3

0

This might nudge you in the right direction:

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

>>> lenall(listlist)
12

This actually counts the total number of lists in the data structure. If you want to add all their lengthes, you would have to change it to:

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

>>> lenall(listlist)
11
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

Here's a possible solution:

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

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

print(length(listlist))

Output

11
Nick
  • 138,499
  • 22
  • 57
  • 95
0

Here is code that should do what you desire.

I included two versions of the function, one for clarity, and one that is smaller but functionally identical.

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

The output is:

12
12
statsman
  • 86
  • 2