0

I want implement a function that combines vertically all the elements of an unknown number of lists in Python. Each list has a different size. E.g., this is the list of list and each row is a list:

A0, A1
B0
C0, C1, C2

Then I would like to print

A0, B0, C0
A0, B0, C1
A0, B0, C2
A1, B0, C0
A1, B0, C1
A1, B0, C2

Note that in the example there are 3 lists but they could also more or less, not necessary 3. My problem is that I don't have idea how to solve it. I tough to implement a recursive method, in which if some condition is satisfied then print the value, else call recursively the function. Here the pseudo-code:

def printVertically(my_list_of_list, level, index):
    if SOME_CONDITION:
        print str(my_list_of_list[index])

    else:
        for i in range (0, int(len(my_list_of_list[index]))):
            printVertically(my_list_of_list, level-1, index)

Here the main code:

list_zero = []
list_zero.append("A0")
list_zero.append("B0")
list_zero.append("C0")

list_one = []
list_one.append("A1")

list_two = []
list_two.append("A2")
list_two.append("B2")

list_three = []
list_three.append("A3")
list_three.append("B3")
list_three.append("C3")
list_three.append("D3")


my_list_of_list = []
my_list_of_list.append(list_zero)
my_list_of_list.append(list_one)
my_list_of_list.append(list_two)
my_list_of_list.append(list_three)


level=int(len(my_list_of_list))
index=0
printVertically(my_list_of_list, level, index)

level is the length of the list of list and index should represent the index of a specific list used when I want to print a specific element. Well, no idea how to proceed. Any hint?

I have searched but in all solutions, people knew the number of lists or the number of elements in each list, like these links:

Link 1

Link 2

Link 3

Community
  • 1
  • 1
Junior hpc
  • 169
  • 1
  • 12

1 Answers1

2

I believe that what you want here is the cross-product of the various sets. You can do this with Python's itertools.product method. Documentation is here. Something like:

import itertools
a_list = ["A0", "A1"]
b_list = ["B0"]
c_list = ["C0", "C1", "C2"]
for combo in itertools.product(a_list, b_list, c_list):
    print combo

Output:

('A0', 'B0', 'C0')
('A0', 'B0', 'C1')
('A0', 'B0', 'C2')
('A1', 'B0', 'C0')
('A1', 'B0', 'C1')
('A1', 'B0', 'C2')

Does that get you moving?


Example with one overarching list:

my_list_list = [a_list, b_list, c_list]
for combo in itertools.product(*my_list_list):
    print combo

... and we get the same output

Prune
  • 76,765
  • 14
  • 60
  • 81
  • I am testing this code but I don't know what I should put instead of `...` for printing all the combinations.. Any hint? Thanks. – Junior hpc Jul 13 '16 at 19:19
  • Exactly what I need! Thanks :) – Junior hpc Jul 13 '16 at 19:29
  • one more question. Instead of specifying each list in the method parameter like this `itertools.product(a_list, b_list, c_list)`, how can I specify only the list of lists? I have tried this `itertools.product(my_list_of_list)` but it does not print any combination. Thanks. – Junior hpc Jul 13 '16 at 20:00
  • ... there you go ... :-) – Prune Jul 13 '16 at 20:39