如何将排列应用于列表?
How can I apply a permutation to a list?
如何让 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
我想要这样的东西 returns(在这个例子中):
['g', 'd', 'a', 'h', 'e', 'b', 'i', 'f', 'c']
我已阅读 https://docs.sympy.org/latest/modules/combinatorics/permutations.html,但不知道如何。
关于如何解决这个问题有什么建议吗?
你可以做到 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']
您的示例输出似乎具有将给定排列的反向应用到列表的结果 - 如果这是您需要的输出,您需要反转最终列表或排列中的每个列表。
来自here:
The permutation can be ‘applied’ to any list-like object, not only Permutations.
如何让 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
我想要这样的东西 returns(在这个例子中):
['g', 'd', 'a', 'h', 'e', 'b', 'i', 'f', 'c']
我已阅读 https://docs.sympy.org/latest/modules/combinatorics/permutations.html,但不知道如何。
关于如何解决这个问题有什么建议吗?
你可以做到 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']
您的示例输出似乎具有将给定排列的反向应用到列表的结果 - 如果这是您需要的输出,您需要反转最终列表或排列中的每个列表。
来自here:
The permutation can be ‘applied’ to any list-like object, not only Permutations.