The problem: I have some list of numbers, like [1,1,2]
. I need to generate the unique permutations. The permutations are [1,1,2],[1,1,2],[1,2,1],[1,2,1],[2,1,1],[2,1,1]
. I need to only generate the unique permutations, i.e., [1,1,2],[1,2,1],[2,1,1]
.
My attempt: My first attempt was to keep a set of existing permutations, and create a filter for the itertools.permutations
generator that would use the set to filter out duplicates. However, for efficiency reasons, I would rather not generate those permutations in the first place. Even for a small list of 12 numbers, only 1% of them are unique.
I have the start of an idea that I can't seem to figure out all the way: I could create permutations of the unique values in my list, i.e., [1,2]
, put the remaining numbers in all the different places.
Thanks for any help, and to be clear, I do not want to filter out duplicate permutations, I want to generate only unique permutations in the first place.