I'm trying to get a list of permutations of x unique characters repeated y times each. So that would look something like this:
x = ['0', '1']
y = 2
permutation_list = ['0011','0101','1001','1010','0110','1100']
I don't want extra stuff ('0001', '1110', etc) and I don't want duplicates - does anyone know of a neat way to do this?
I've tried using itertools but I end up with duplicates.
Use all unique permutations of your list, repeated y
times:
from itertools import permutations
permutation_list = set(permutations(x * y))
Demo:
>>> from itertools import permutations
>>> x = ['0', '1']
>>> y = 2
>>> set(permutations(x * 2))
{('0', '1', '1', '0'), ('0', '1', '0', '1'), ('1', '0', '1', '0'), ('1', '1', '0', '0'), ('1', '0', '0', '1'), ('0', '0', '1', '1')}
These can be mapped back to a list:
[''.join(combo) for combo in set(permutations(x * 2))]