In Python, it is quite simple to produce all permutations of a list using the itertools
module. I have a situation where the sequence I'm using only has two characters (i.e. '1122'
). I want to generate all unique permutations.
For the string '1122'
, there are 6 unique permutations (1122
, 1212
, 1221
, etc), but itertools.permutations
will yield 24 items. It's simple to only record the unique permutations, but it will take much longer than necessary to collect them as all 720 items are considered.
Is there a function or module that accounts for repeated elements when generating permutations so I don't have to write my own?
Best Answer
This web page looks promising.
2017-08-12
Seven years later, here is a better algorithm (better for clarity):