itertools.permutations 生成其元素根据其位置而不是其值被视为唯一的位置。所以基本上我想避免这样的重复:
>>> list(itertools.permutations([1, 1, 1]))
[(1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1), (1, 1, 1)]
事后过滤是不可能的,因为在我的情况下排列的数量太大。
有人知道合适的算法吗?
非常感谢你!
编辑:
我基本上想要的是以下内容:
x = itertools.product((0, 1, 'x'), repeat=X)
x = sorted(x, key=functools.partial(count_elements, elem='x'))
这是不可能的,因为sorted
创建了一个列表并且 itertools.product 的输出太大。
对不起,我应该描述实际问题。