permutations with unique values

前端 未结 19 1432
爱一瞬间的悲伤
爱一瞬间的悲伤 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:33

    A naive approach might be to take the set of the permutations:

    list(set(it.permutations([1, 1, 1])))
    # [(1, 1, 1)]
    

    However, this technique wastefully computes replicate permutations and discards them. A more efficient approach would be more_itertools.distinct_permutations, a third-party tool.

    Code

    import itertools as it
    
    import more_itertools as mit
    
    
    list(mit.distinct_permutations([1, 1, 1]))
    # [(1, 1, 1)]
    

    Performance

    Using a larger iterable, we will compare the performances between the naive and third-party techniques.

    iterable = [1, 1, 1, 1, 1, 1]
    len(list(it.permutations(iterable)))
    # 720
    
    %timeit -n 10000 list(set(it.permutations(iterable)))
    # 10000 loops, best of 3: 111 µs per loop
    
    %timeit -n 10000 list(mit.distinct_permutations(iterable))
    # 10000 loops, best of 3: 16.7 µs per loop
    

    We see more_itertools.distinct_permutations is an order of magnitude faster.


    Details

    From the source, a recursion algorithm (as seen in the accepted answer) is used to compute distinct permutations, thereby obviating wasteful computations. See the source code for more details.

提交回复
热议问题