'Given 2 lists (list_1, list_2) containing numbers 0 to (n-1), how do I obtain the permutation p (an element of S(n)) such that p(list_1) = list_2?

I have been using the sympy permutations package. So far I have declared permutations as follows

from sympy.combinatorics.generators import symmetric, Permutation
p = Permutation([[2, 3], [5]])
print(p(([0, 1, 2, 3, 4, 5]))

out:[0, 1, 3, 2, 4, 5]

I would like to declare a permutation given 2 lists. For example, I would like the element

I would like the permutation to act on integers rather than positions in the list (so that (01) * [1, 2, 3, 0] = [0, 2, 3, 1], instead of (01) * [1, 2, 3, 0] = [2, 1, 3, 0])

How can I do this ?



Solution 1:[1]

See the discussion about composition in permutations.py:

>>> start = Permutation([1, 0, 2, 3])
>>> finish = Permutation([2, 3, 1, 0])
>>> p = finish*start
>>> p([1,0,2,3])
[2, 3, 1, 0]
>>> p
Permutation(0, 2)(1, 3)

Maybe a better way to do this, but this seems to work if you want to access by name instead of position:

from sympy.combinatorics import Permutation
from sympy.utilities.iterables import permutations
P = Permutation
start = [1,0,2,3]
end = [2,3,1,0]
do = P(end)*P(start)
def byname(l):
    a = do([l.index(i) for i in range(len(l))])
    return [a.index(i) for i in range(len(l))]

for i in permutations(range(4)):
  i, byname(i)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1