I want to generate all possible lists of [“x”,”y”,”z”,1,2,3] that are ordered in ascending order. “x”, “y” and “z” can be any real numbers and so their order can be arbitrary. However, as 1<2<3, 1 must be before 2 and 2 must be before 3. So, for example, the permutation [“x”, 1, “y”, “z”, 2, 3] should be generated, but the permutation [“x”, 1, “y”, “z”, 3, 2] should not be generated.
The simplest solution is
for p in itertools.permutations(["x","y","z",1,2,3]):
if permutation_satisfies_the_constraint(p):
yield p
but it is very inefficient, as it would generate many unneeded permutations
(the total number of permutations of 6 elements is 6!=720, but the total number of legal permutations is 6!/3!=120).
What is an efficient way to generate only the permutations that satisfy the constraint on 1,2,3?