permutations with unique values

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

    What about

    np.unique(itertools.permutations([1, 1, 1]))
    

    The problem is the permutations are now rows of a Numpy array, thus using more memory, but you can cycle through them as before

    perms = np.unique(itertools.permutations([1, 1, 1]))
    for p in perms:
        print p
    

提交回复
热议问题