Inverting permutations in Python

前端 未结 7 1118
余生分开走
余生分开走 2021-01-12 21:08

I\'m new to programming, and I\'m trying to write a Python function to find the inverse of a permutation on {1,2,3,...,n} using the following code:

def inv(s         


        
相关标签:
7条回答
  • 2021-01-12 21:51

    Maybe there is a shorter way:

    def invert(p):
        return [p.index(l) for l in range(len(p))] 
    

    so that:

    perm = [3, 0, 2, 1]; print(invert(perm))
    

    returns

    [1,3,2,0]

    0 讨论(0)
提交回复
热议问题