I want to generate a list of all permutations in an array that contains only 1s and 0s with no repetitions. I have used the code
import itertools
import numpy as np
L = 3
M = 2
list1=list(itertools.product([0,1],repeat=L+M-1))
list1_filtered=np.asarray(list(filter(lambda x: sum(x)==M,list1)))
print(list1_filtered)
and the output is
[[0 0 1 1]
[0 1 0 1]
[0 1 1 0]
[1 0 0 1]
[1 0 1 0]
[1 1 0 0]]
Is there any way to make the generation faster (i.e. without using the filter operation to select only the array with total sum equal to M)?
Is it possible to create an iterative function f that starting from [0,0,1,1] gives recursively all the other rows? namely
f([0,0,1,1]) = [0,1,0,1],
f([0,1,0,1]) = [0,1,1,0],
....