我正在使用
Combinatorics
库中的permutations
函数处理一个含有多个重复值的列表。我的问题在于,permutations
函数会创建所有排列,导致内存溢出,即使其中许多排列是相同的。julia> collect(permutations([1, 1, 2, 2], 4))
24-element Array{Array{Int64,1},1}:
[1, 1, 2, 2]
[1, 1, 2, 2]
[1, 2, 1, 2]
[1, 2, 2, 1]
[1, 2, 1, 2]
[1, 2, 2, 1]
[1, 1, 2, 2]
[1, 1, 2, 2]
[1, 2, 1, 2]
[1, 2, 2, 1]
[1, 2, 1, 2]
[1, 2, 2, 1]
[2, 1, 1, 2]
[2, 1, 2, 1]
[2, 1, 1, 2]
[2, 1, 2, 1]
[2, 2, 1, 1]
[2, 2, 1, 1]
[2, 1, 1, 2]
[2, 1, 2, 1]
[2, 1, 1, 2]
[2, 1, 2, 1]
[2, 2, 1, 1]
[2, 2, 1, 1]
大量相同的值。我想要的是仅具有唯一排列,而无需首先生成所有排列:
julia> unique(collect(permutations([1, 1, 2, 2], 4)))
6-element Array{Array{Int64,1},1}:
[1, 1, 2, 2]
[1, 2, 1, 2]
[1, 2, 2, 1]
[2, 1, 1, 2]
[2, 1, 2, 1]
[2, 2, 1, 1]
我可以理解 permutations
应该始终返回所有排列,无论是否唯一,但有没有一种方法只生成唯一的排列,这样我就不会耗尽内存?
permutations
returns an iterator, so there should be no problem with memory, unless youcollect
it. Do you really need to do that? Did you tryunique(permutations(
- DNFunique(permutations(...))
? - DNF