permutations with unique values

前端 未结 19 1679
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 01:53

itertools.permutations generates where its elements are treated as unique based on their position, not on their value. So basically I want to avoid duplicates like this:

19条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 02:36

    The best solution to this problem I have seen uses Knuth's "Algorithm L" (as noted previously by Gerrat in the comments to the original post):
    http://stackoverflow.com/questions/12836385/how-can-i-interleave-or-create-unique-permutations-of-two-stings-without-recurs/12837695

    Some timings:

    Sorting [1]*12+[0]*12 (2,704,156 unique permutations):
    Algorithm L → 2.43 s
    Luke Rahne's solution → 8.56 s
    scipy.multiset_permutations() → 16.8 s

提交回复
热议问题