简体   繁体   中英

How can I apply a permutation to a list?

How one might get Sympy Permutation to act on a list? Eg,

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

I'd like something like this that returns (in this example):

['g', 'd', 'a', 'h', 'e', 'b', 'i', 'f', 'c']

I have read https://docs.sympy.org/latest/modules/combinatorics/permutations.html , and don't see how.

Any suggestions as to how might one go about this?

You can just do 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']

Your example output seems to have the result of applying the reverse of the given Permutation to the list - if that is your required output you need to either reverse the final list or each list within the permutation.

From here :

The permutation can be 'applied' to any list-like object, not only Permutations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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