python sort a list using defined order

Solutions on MaxInterview for python sort a list using defined order by the best coders in the world

showing results for - "python sort a list using defined order"
Taylor
13 Jun 2020
1>>> sorted(A, key = lambda i: B.index(i[0]))
2[[6, 1], [1, 3], [3, 5]]
3
Marco
10 Feb 2017
1>>> A = [[3,5],[1,3],[6,1]]
2>>> B = [6,1,3]
3>>> srt = {b: i for i, b in enumerate(B)}
4>>> sorted(A, key=lambda x: srt[x[0]])
5[[6, 1], [1, 3], [3, 5]]
6