如何将排列应用于列表?

Par*_*der 3 python list permutation sympy python-3.x

如何让 Sympy Permutation 在列表上起作用?例如,

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
Run Code Online (Sandbox Code Playgroud)

我想要这样的返回(在这个例子中):

['g', 'd', 'a', 'h', 'e', 'b', 'i', 'f', 'c']
Run Code Online (Sandbox Code Playgroud)

我已经阅读了https://docs.sympy.org/latest/modules/combinatorics/permutations.html,但不知道如何。

关于如何解决这个问题的任何建议?

CDJ*_*DJB 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']
Run Code Online (Sandbox Code Playgroud)

您的示例输出似乎具有将给定排列的反转应用于列表的结果 - 如果这是您所需的输出,则需要反转最终列表或排列中的每个列表。

这里

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