Ask Your Question
0

apply Permutation to a letters list

asked 2024-11-19 16:44:05 +0100

ortollj gravatar image

Hi

Sorry for this very basic question, but how to apply the Permutation function to a list of letters ? I think it should exist a simplest way than the the cooking below. I searched the web, without success !

def applyGen(ge,dic) :   
    return [ge[dic.get(e)] for e in dic.keys() ]

def decrement_dic(dic): 
    return {k - 1: v - 1 for k, v in dic.items()} 

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

test=Permutation((1,5,8,4),(2,6,7,3))
dicD=decrement_dic(test.dict())
applyGen(lettersL,dicD)
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2024-11-19 17:17:37 +0100

FrédéricC gravatar image

updated 2024-11-19 18:28:45 +0100

Like this

sage: L = ['a','b','c','d','e','f','g','h']
sage: test = Permutation((1,5,8,4),(2,6,7,3))
sage: [L[test(i + 1) - 1] for i in range(len(L))]
['e', 'b', 'c', 'a', 'h', 'f', 'g', 'd']

Note that Python numbering starts from 0.

edit flag offensive delete link more

Comments

ok thank you @FrédéricC

ortollj gravatar imageortollj ( 2024-11-19 18:26:40 +0100 )edit

Btw, one can replace test(i + 1) with test[i].

Max Alekseyev gravatar imageMax Alekseyev ( 2024-11-20 03:27:17 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2024-11-19 16:44:05 +0100

Seen: 67 times

Last updated: yesterday