如何在列表中应用置换?

3
如何让Sympy排列作用于一个列表?例如:
from sympy.combinatorics import Permutation

lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
perm = Permutation([[0, 2, 8, 6], [1, 5, 7, 3]])
# Then something like...
perm * lst   # This doesn't work. Throws AttributeError because of list

我想要像这样的东西,它会返回(在此示例中):
['g', 'd', 'a', 'h', 'e', 'b', 'i', 'f', 'c']

我已阅读https://docs.sympy.org/latest/modules/combinatorics/permutations.html,但并不清楚如何操作。

有什么建议可以帮助我吗?

1个回答

5
你可以直接执行 perm(lst)
>>> from sympy.combinatorics import Permutation
>>> lst = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
>>> perm = Permutation([[0, 2, 8, 6], [1, 5, 7, 3]])
>>> perm(lst)
['c', 'f', 'i', 'b', 'e', 'h', 'a', 'd', 'g']

你的示例输出似乎是将给定置换的反转应用于列表的结果 - 如果这是您需要的输出,则需要反转最终列表或置换中的每个列表。
来自这里

置换可以“应用”于任何类似于列表的对象,而不仅仅是置换。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接