Say I have three lists:
X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1]
Z = [ 5, 2, 3, 1, 4, 9, 6, 0, 7]
I can sort X according to Y rather easily:
XY_sort = [x for _,x in sorted(zip(Y,X))]
print(XY_sort) # ["a", "d", "h", "b", "c", "e", "i", "f", "g"]
Doubts arise when I want to sort list Z according to the sorting that produced XY_sort. Ideally, I want to end up with:
Z_sort = [5, 1, 0, 2, 3, 4, 7, 9, 6]
I'm guessing the best way to do this would be to somehow store the sorted indices of X when it gets sorted into XY_sort, then using those to sort Z, but I don't know how I would go about doing this. Any help would be greatly appreciated!