简体   繁体   English

使用Python和Sympy以可预测的顺序生成唯一的排列

[英]Generating unique permutations in a predictable order with Python and Sympy

I have a list of elements in python. 我在python中有一个元素列表。 In the final code, the length of the list will be variable and the elements of my list much more lengthy, but I can demonstrate my question with three dummy elements. 在最终的代码中,列表的长度是可变的,列表的元素更加冗长,但我可以用三个虚拟元素来演示我的问题。

Basically, for any list of length n, there will be n-1 identical elements, and 1 unique element. 基本上,对于任何长度为n的列表,将有n-1个相同的元素和1个唯一元素。

So, in the example of three elements I have: 所以,在我有三个要素的例子中:

test = ['b', 'a', 'a']

For small cases such as 3, where I can verify by eye the order of the elements, I have been using a function from the Sympy module, as below: 对于像3这样的小案例,我可以通过眼睛验证元素的顺序,我一直在使用Sympy模块中的函数,如下所示:

permutations = list(multiset_permutations(test))

However, once cases become too large, I'm not certain that the order will be predictable. 但是,一旦案件变得太大,我不确定订单是否可预测。 And, checking the official documentations doesn't really clarify the issue for me very well. 而且,检查官方文件并没有真正为我澄清这个问题。

Is there a way to generate these permutations in a predictable order such that I could know, for example: 有没有办法以可预测的顺序生成这些排列,以便我可以知道,例如:

permutations[0] = ['b', 'a', 'a']
permutations[1] = ['a', 'b', 'a']
permutations[2] = ['a', 'a', 'b']

Thank you for any help that can be given. 感谢您提供的任何帮助。

The elements are ordered before the permutations are generated by multiset_permutations . multiset_permutations生成排列之前,元素是有序的。 Your result will not depend on the order of the input items. 您的结果将不依赖于输入项的顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM