Issue
I have a list of lists that I want to sort in the same order as another list of lists:
l1 = [['hello', 'test'], ['there', 'correct'], ['Elem3', 'yes']]
l2 = [['hello'], ['Elem3'], ['there']]
So I want to rearrange l1 so it's in the same order as l2 . (Note: This is just a simple test case. The actual list I'm dealing with has several elements in it.
e.g. the result should be:
l1 = [['hello', 'test'], ['Elem3', 'yes'], ['there', 'correct']]
Using python2 (enterprise)
Solution
Use python's built-in sort
:
l1.sort(key=lambda pair: l2.index(pair[0]))
Answered By - jonathan.scholbach
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.