Reputation: 47
How can I create all possible permutations where the order of the item in the list matters?
Example:
ListA = [1a, 2a, 3a, 4a]
ListB = [1b, 2b, 3b, 4b]
ListC = [1c, 2c, 3c, 4c]
The output should produce lists of 4 items, such as:
1a, 2b, 3c, 4c
or
1c, 2c, 3b, 4a
And the output should produce all possible permutations/combinations of these items, where ORDER of the items in the list matches the original order.
The first item in all the permutations must be one of the first items of the original list. The second item must be one of the second items, etc. 2a, 2b, 2c will never appear in the first or third position in the new list.
Upvotes: 0
Views: 110
Reputation: 195438
Try:
from itertools import product
ListA = ["1a", "2a", "3a", "4a"]
ListB = ["1b", "2b", "3b", "4b"]
ListC = ["1c", "2c", "3c", "4c"]
for t in product(*zip(ListA, ListB, ListC)):
print(t)
Prints:
('1a', '2a', '3a', '4a')
('1a', '2a', '3a', '4b')
('1a', '2a', '3a', '4c')
('1a', '2a', '3b', '4a')
('1a', '2a', '3b', '4b')
('1a', '2a', '3b', '4c')
('1a', '2a', '3c', '4a')
...
('1c', '2c', '3b', '4c')
('1c', '2c', '3c', '4a')
('1c', '2c', '3c', '4b')
('1c', '2c', '3c', '4c')
Upvotes: 1