Home > database >  How can iterate over lists to get their data using recursion?
How can iterate over lists to get their data using recursion?

Time:09-08

I have a process in which nested lists are generated, something like a tree of nodes, but nodes can be lists of data and each list can contain a sublist, etc.

I just need to print the value of each list, but I need to solve this using recursion because, the position and the size of each list change every time. So, all is dynamic.

Maybe you are wondering why I need this, in fact I have a more complex process related with an assembly process, so this is the easiest way to represent that process.

my_list = [ "C0", "C1", ["A0-C0","A0-C1","A0-C2", ["A01-C0","A01-C1"], ["A02-C0"], ["A03-C0","A03-C1","A03-C2","A03-C3"]], ["A1-C0","A1-C1","A1-C2","A1-C3","A1-C4", ["A10-C0"], ["A11-C0","A11-C1"] ]  ]

print("my_list size: "   str(len(my_list))   "\n")

print("")
print(my_list[0])
print(my_list[1])

print("")
print("list - size: "   str(len(my_list[2])))
print(my_list[2][0])
print(my_list[2][1])
print(my_list[2][2])

print("")
print("list - size: "   str(len(my_list[2][3])))
print(my_list[2][3][0])
print(my_list[2][3][1])

print("")
print("list - size: "   str(len(my_list[2][4])))
print(my_list[2][4][0])

print("")
print("list - size: "   str(len(my_list[2][5])))
print(my_list[2][5][0])
print(my_list[2][5][1])
print(my_list[2][5][2])
print(my_list[2][5][3])

print("")
print("list - size: "   str(len(my_list[3])))
print(my_list[3][0])
print(my_list[3][1])
print(my_list[3][2])
print(my_list[3][3])
print(my_list[3][4])

print("")
print("list size: "   str(len(my_list[3][5])))
print(my_list[3][5][0])

print("")
print("list size: "   str(len(my_list[3][6])))
print(my_list[3][6][0])
print(my_list[3][6][1])

output:

my_list size: 4


C0
C1

list - size: 6
A0-C0
A0-C1
A0-C2

list - size: 2
A01-C0
A01-C1

list - size: 1
A02-C0

list - size: 4
A03-C0
A03-C1
A03-C2
A03-C3

list - size: 7
A1-C0
A1-C1
A1-C2
A1-C3
A1-C4

list size: 1
A10-C0

list size: 2
A11-C0
A11-C1

I need to solve this using recursion and for loops, in order to iterate on each list. Maybe could be complex algorithms to achieve this, but due to my process I can only use for loops.

Any idea or suggestion to solve this? Thanks in advance!

CodePudding user response:

You can get similar output with a function like this:

def print_nested_list(obj):
    if isinstance(obj, str):
        print(obj)
    else:
        print("")
        print(f"list - size: {len(obj)}")
        for item in obj:
            print_nested_list(item)

my_list = [ "C0", "C1", ["A0-C0","A0-C1","A0-C2", ["A01-C0","A01-C1"], ["A02-C0"], ["A03-C0","A03-C1","A03-C2","A03-C3"]], ["A1-C0","A1-C1","A1-C2","A1-C3","A1-C4", ["A10-C0"], ["A11-C0","A11-C1"] ] ]
print_nested_list(my_list)

The output:

list - size: 4
C0
C1

list - size: 6
A0-C0
A0-C1
A0-C2

list - size: 2
A01-C0
A01-C1

list - size: 1
A02-C0

list - size: 4
A03-C0
A03-C1
A03-C2
A03-C3

list - size: 7
A1-C0
A1-C1
A1-C2
A1-C3
A1-C4

list - size: 1
A10-C0

list - size: 2
A11-C0
A11-C1

This assumes that any objects the algorithm will encounter are either strings or iterables (such as lists) containing strings.

CodePudding user response:

Something like that should work

def print_lists(lst):
    if isinstance(lst, list):
        print(f'\nlist - size: {len(lst)}')
        for l in lst:
            print_lists(l)
    else:
        print(lst)

print_lists(my_list)

Output

list - size: 4
C0
C1

list - size: 6
A0-C0
A0-C1
A0-C2

list - size: 2
A01-C0
A01-C1

list - size: 1
A02-C0

list - size: 4
A03-C0
A03-C1
A03-C2
A03-C3

list - size: 7
A1-C0
A1-C1
A1-C2
A1-C3
A1-C4

list - size: 1
A10-C0

list - size: 2
A11-C0
A11-C1
  • Related