Reputation: 13
So basically, I want to generate all possible combinations of 5 lists, though for simplification I will only go for 3 right now. These are the lists:
five=["fivea","fiveb","fivec","fived"]
six=["sixa","sixb","sixc","sixd"]
seven=["sevena","sevenb","sevenc","sevend"]
But all the solutions I found were combining them in a way where combinations like
"fivea","sixa","sevena"
would be allowed. The way I need it though would be a combination where all Arrays stay complete, just in a different order, and none could be left away.
Upvotes: 0
Views: 1431
Reputation: 11903
It isn't totally clear what your desired outcome is, but I think itertools.product()
is what you are looking for...
from itertools import product
five=["fivea","fiveb","fivec","fived"]
six=["sixa","sixb","sixc","sixd"]
seven=["sevena","sevenb","sevenc","sevend"]
my_lists = [five, six, seven]
for item in product(*my_lists):
print(item)
('fivea', 'sixa', 'sevena')
('fivea', 'sixa', 'sevenb')
('fivea', 'sixa', 'sevenc')
('fivea', 'sixa', 'sevend')
('fivea', 'sixb', 'sevena')
('fivea', 'sixb', 'sevenb')
('fivea', 'sixb', 'sevenc')
('fivea', 'sixb', 'sevend')
('fivea', 'sixc', 'sevena')
('fivea', 'sixc', 'sevenb')
('fivea', 'sixc', 'sevenc')
('fivea', 'sixc', 'sevend')
('fivea', 'sixd', 'sevena')
('fivea', 'sixd', 'sevenb')
('fivea', 'sixd', 'sevenc')
('fivea', 'sixd', 'sevend')
('fiveb', 'sixa', 'sevena')
('fiveb', 'sixa', 'sevenb')
...
('fived', 'sixd', 'sevend')
from itertools import permutations as p
five=["fivea","fiveb"]
six=["sixa","sixb","sixc"]
seven=["sevena","sevenb"]
my_lists = [five, six, seven]
# flatten it out...
my_items = [item for sublist in my_lists for item in sublist ]
print(my_items)
for perm in p(my_items, len(my_items)):
print(perm)
Upvotes: 5