Below is the use-case I am trying to solve:
I have 2 lists of lists: (l
and d
)
In [1197]: l
Out[1197]:
[['Cancer A', 'Ecog 9', 'Fill 6'],
['Cancer B', 'Ecog 1', 'Fill 1'],
['Cancer A', 'Ecog 0', 'Fill 0']]
In [1198]: d
Out[1198]: [[100], [200], [500]]
It's a 2-part problem here:
- Sort
l
based on the priority of values. eg:Cancer
,Ecog
andFill
(in this casekey=(0,1,2)
). It could be anything likeEcog
,Cancer
,Fill
so, key=(1,0,2). - Sort
d
in the same order in whichl
has been sorted int above step.
Step #1 I'm able to achieve, like below:
In [1199]: import operator
In [1200]: sorted_l = sorted(l, key=operator.itemgetter(0,1,2))
In [1201]: sorted_l
Out[1200]:
[['Cancer A', 'Ecog 0', 'Fill 0'],
['Cancer A', 'Ecog 9', 'Fill 6'],
['Cancer B', 'Ecog 1', 'Fill 1']]
Now, I want to sort values of d
in the same order as the sorted_l
.
Expected output:
In [1201]: d
Out[1201]: [[500], [100], [200]]
What is the best way to do this?