Find all ordered combinations of two lists in python
Question:
I am trying to have a python function that will return all possible ordered combinations between two lists.
Ex
a = [1, 2, 3]
b = [4, 5, 6]
output:
123
126
153
156
423
426
453
456
The number of combinations returned should be 2^n where n is the length of the lists. The lists will always be the same length. The order of the lists must always remain the same. In the example: 1 or 4 must always be in the first position, 2 or 5 must always be in the second position, and 3 or 6 must always be in the third position.
UPDATED
I have tried doing list(itertools.combinations(a + b, 3)), however, this returns all combinations including those that I do not want (ex 125. The order must be preserved so that only 1 or 4 can be in the first position, only 2 or 5 can be in the second position, and only 3 or 6 can be in the last position.
My issue is in actually creating the list of possible outputs. Once I have a list I can mess with it to display the way I want. (eventually, I want to output to CSV file).
Answers:
A combination of zip
(to make the possibilities for each position) and itertools.product
(to cycle the possibilities at each position) makes this pretty easy:
from itertools import product
a = [1, 2, 3]
b = [4, 5, 6]
for result in product(*zip(a, b)):
print(*result, sep='')
zip
is just making pairs of (1, 4)
, (2, 5)
and (3, 6)
; unpacking those as three arguments to product
makes product
cycle through each possible selection of a single value from each pair, producing:
123
126
153
156
423
426
453
456
I am trying to have a python function that will return all possible ordered combinations between two lists.
Ex
a = [1, 2, 3]
b = [4, 5, 6]
output:
123
126
153
156
423
426
453
456
The number of combinations returned should be 2^n where n is the length of the lists. The lists will always be the same length. The order of the lists must always remain the same. In the example: 1 or 4 must always be in the first position, 2 or 5 must always be in the second position, and 3 or 6 must always be in the third position.
UPDATED
I have tried doing list(itertools.combinations(a + b, 3)), however, this returns all combinations including those that I do not want (ex 125. The order must be preserved so that only 1 or 4 can be in the first position, only 2 or 5 can be in the second position, and only 3 or 6 can be in the last position.
My issue is in actually creating the list of possible outputs. Once I have a list I can mess with it to display the way I want. (eventually, I want to output to CSV file).
A combination of zip
(to make the possibilities for each position) and itertools.product
(to cycle the possibilities at each position) makes this pretty easy:
from itertools import product
a = [1, 2, 3]
b = [4, 5, 6]
for result in product(*zip(a, b)):
print(*result, sep='')
zip
is just making pairs of (1, 4)
, (2, 5)
and (3, 6)
; unpacking those as three arguments to product
makes product
cycle through each possible selection of a single value from each pair, producing:
123
126
153
156
423
426
453
456