开发者

Python permutation difference

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜