Let us approach the problem in the following steps:
1. Build a list of all possible allowed characters
We can do this very easily using the function below:
def flatten_list(main_list):
all_elems = []
for inner_list in main_list:
for elem in inner_list:
all_elems.append(elem)
return list(set(all_elems))
all_elems = flatten_list(main_list)
print (all_elems)
>>>
['H', 'I', 'C', 'B', 'D', 'F', 'A', 'G', 'E']
2. Build the possible combinations of all the first inner list
There are 9 elements in total
For the first list, there will be 9C3 options = 84
We can build this using combinations function as:
from itertools import combinations
first_list_candidates = list(combinations(all_elems, len(main_list[0])))
print (len(first_list_candidates ))
>>> 84
3. Build the other inner lists
Now since you do not want repetitions, the way to build the other lists is:
For each combo in first list:
check which elems are remaining
construct second list using these elems
For each combo in second list
check which elems are remaining
construct third list using these elems
Append the combinations into a new list
In python:
all_results = []
### for each combination in first list
for combo1_ in first_list_candidates:
### check which remaining elems we can pick
remaining_elems = [elem for elem in all_elems if elem not in combo1_]
### get all candidates for second list from these remaining elems
second_list_candidates = list(combinations(remaining_elems, len(main_list[1])))
### for each combination in second list
for combo2_ in second_list_candidates:
### check which remaining elems we can pick
remaining_elems_2 = [elem for elem in remaining_elems if elem not in combo2_]
### get all candidates for third list from these remaining elems
third_list_candidates = list(combinations(remaining_elems_2, len(main_list[2])))
### append the results
for combo3_ in third_list_candidates:
all_results.append([combo1_, combo2_, combo3_])
As someone in the comments mentioned there are 1260 possible combinations
len(all_results)
>>>
1260
Hope this helped, I am sure there is a more optimized solution though...
[["A","H","G"],["D","B","F","C"],["E","I"]]and[["H","G","A"],["D","B","F","C"],["E","I"]]the same outcome or different (note the position ofA)?