Monday, November 22, 2010

Generate permutations of list with repeated elements

Programmer Question

In Python, it is quite simple to produce all permutations of a list using the itertools module. I have a situation where the list I'm using only has two characters (i.e. '111222'). I want to generate all unique permutations.



For the string '111222', there are 20 permutations, but itertools.permutations will yield 720 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?



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails