Forgive my ignorance. I have a brain fart at the moment and unable to come up with a solution. Let's say I have a list of [1, 1, 0, 0]
. I want to calculate all the four digits binary numbers that have exactly two 1s and two zeros, like:
['0110', '0011', '0101', '1100', '1010', '1001']
This works:
from itertools import permutations
set([''.join(x) for x in list(permutations('0011', 4))])
But this calculate the entire permutations and then discards the duplicate. Meaning, it calculates 24 times but I only need 6. It is much more significant if the collection is [1, 1, 1, 1, 0, 0, 0, 0]
.
This should be very easy but I just can't wrap my head around it.