独特的排列生成器? [英] Unique permutation generator?
问题描述
问题:我有一些数字列表,例如[1,1,2]
.我需要生成唯一的排列.排列为[1,1,2],[1,1,2],[1,2,1],[1,2,1],[2,1,1],[2,1,1]
.我只需要生成唯一的排列,即[1,1,2],[1,2,1],[2,1,1]
.
The problem: I have some list of numbers, like [1,1,2]
. I need to generate the unique permutations. The permutations are [1,1,2],[1,1,2],[1,2,1],[1,2,1],[2,1,1],[2,1,1]
. I need to only generate the unique permutations, i.e., [1,1,2],[1,2,1],[2,1,1]
.
我的尝试:我的第一个尝试是保留一组现有排列,并为itertools.permutations
生成器创建一个过滤器,该过滤器将使用该过滤器过滤出重复项.但是,出于效率考虑,我宁愿不首先生成这些排列.即使只列出一小部分12个数字,其中只有1%是唯一的.
My attempt: My first attempt was to keep a set of existing permutations, and create a filter for the itertools.permutations
generator that would use the set to filter out duplicates. However, for efficiency reasons, I would rather not generate those permutations in the first place. Even for a small list of 12 numbers, only 1% of them are unique.
我开始似乎无法完全理解:我可以在列表中创建唯一值的排列,即[1,2]
,将其余数字放在所有不同的位置
I have the start of an idea that I can't seem to figure out all the way: I could create permutations of the unique values in my list, i.e., [1,2]
, put the remaining numbers in all the different places.
感谢您的帮助,要明确地说,我不想过滤掉重复的排列,我只想首先生成唯一的排列.
Thanks for any help, and to be clear, I do not want to filter out duplicate permutations, I want to generate only unique permutations in the first place.
推荐答案
我从我的笔记本电脑无法使用set(permutations(seq))
方法执行长度为11的输入序列,但是使用此方法可以!
My laptop can't do length-11 input sequences with the set(permutations(seq))
method, but with this method it can!
这篇关于独特的排列生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!