Is there a simple way to sort a list X in python using two other lists Y,Z like so?
before sorting:
X = ["a", "b", "c", "d"]
Y = [ 10, 1, 2, 16 ] #positive list
Z = [ 5, 6, 7, 10 ] #negative list
creating list W to assist with understanding
W = [ 5, -5, -5, 6 ] # create new list total (Y-Z) positive - negative values.
This is what I have done so far, sorted X according to W the total of Y,Z (Y-Z) in descending
- Sort X based on W (total) in descending order
W, X = zip(*sorted(zip(W, X),reverse=True))
X = ["d", "a", "b", "c"]
W = [ 6, 5, -5, -5 ]
What I would like to do further is conditionally sort if values of W are equal, I'd like to sort based on highest value in list Y, lowest value in list Z and choosing the maximum.
X = ["a", "b", "c", "d"]
Y = [ 10, 1, 2, 16 ] #positive list
Z = [ 5, 6, 7, 10 ] #negative list
W = [ 5, -5, -5, 6 ] # create new list total (Y-Z) positive - negative values.
here, indexes 1,2 are tied in W because W[1]==W[2]=-5 so we compare k1=max(Y[1],Y[2])=max(1,2)=2 and k2=min(Z[1],Z[2])=min(6,7)=6
- since k2>k1, we prioritize ordering of X based on Z
- if k1>k2 we should prioritize ordering of X based on Y. final result must be,
X = ["d", "a", "b", "c"]
How could we do this as simple as possible even for huge lists with n number of same values in the list W?