开发者

Python permutation difference

开发者 https://www.devze.com 2023-03-19 16:09 出处:网络
Is there something built-in to tell the permutation difference between two list?E.g. difference(\"ijk\", \"ikj\") = (0,2,1开发者_如何学Go)

Is there something built-in to tell the permutation difference between two list? E.g.

difference("ijk", "ikj") = (0,2,1开发者_如何学Go)
difference("jik", "ikj") = (2,0,1)
etc ...

oops, nevermind, here is a snippet

[a.index(i) for i in b]


Pretty simple...

def difference(after, before):
    return tuple(before.index(x) for x in after)

print difference('ijk', 'ikj') # (0, 2, 1)
print difference('jik', 'ikj') # (2, 0, 1)


Is this short enough?

tuple([s2.index(p) for p in s1])
0

精彩评论

暂无评论...
验证码 换一张
取 消