I have a list of possible choices: [[1], [2, 4], [4], [5, 6, 2], [5, 3]]
.
I want to list all combinations, taking maximum one element from each sublist, without repeating elements.
So [1, 2, 4, 5, 3]
is a valid option. But [1, 4, 4, 5, 3]
is not. I allow not making a choice in any sublist, so [1,4, None,5,3]
is valid, as in [1, None, None, None, None]
, and [None, None, None, None, None]
.
I can't simply enumerate all combinations then filter out the ones I don't want, since for a large list of possible choices, it would quickly become computationally infeasible (I'm looking at 25^25 maximum combinations in my project).
edit: I would also apply some additional criteria to the results, such as filtering to have no more than a threshold of None
choices, or sorting the resultant list of combinations in order of combinations with fewest None
choices.
edit: with details of the real-life case: I'd like to apply it to a list of 25 sublists, each of which can have 1-25 elements. Realisitically, each sublist will have max 15 elements, with 2-4 on average.
So the easy solution of list(itertools.product(*choices))
then filtering is out.
I may also wish to add other filter conditions to the list of combinations, so ideally I can filter these upfront.
I've tried building a tree recursively, where e.g. root node has the full list of choices, child node makes the first choice [1], and has an updated list of choices where '1' is removed from all list[1:] choices.
Struggling to implement the recursion though.
Can you help me with any other approaches?
[None, 2, None, None, 3, ..., 20, None]
? – FonsNone
choices, or some other criteria, and apply it to my grid so that I have a good combination of rows/columns. – Acetaldehyde