I have a list of strings like this:
X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1 ]
What is the shortest way of sorting X using values from Y to get the following output?
["a", "d", "h", "b", "c", "e", "i", "f", "g"]
The order of the elements having the same "key" does not matter. I can resort to the use of for
constructs but I am curious if there is a shorter way. Any suggestions?
Use the zip() and sorted() Functions to Sort the List Based on Another List in Python. In this method, we will use the zip() function to create a third object by combining the two given lists, the first which has to be sorted and the second on which the sorting depends.
Shortest Code
[x for _, x in sorted(zip(Y, X))]
Example:
X = ["a", "b", "c", "d", "e", "f", "g", "h", "i"] Y = [ 0, 1, 1, 0, 1, 2, 2, 0, 1] Z = [x for _,x in sorted(zip(Y,X))] print(Z) # ["a", "d", "h", "b", "c", "e", "i", "f", "g"]
Generally Speaking
[x for _, x in sorted(zip(Y, X), key=lambda pair: pair[0])]
Explained:
zip
the two list
s.list
based on the zip
using sorted()
.list
.For more information on how to set\use the key
parameter as well as the sorted
function in general, take a look at this.
Zip the two lists together, sort it, then take the parts you want:
>>> yx = zip(Y, X) >>> yx [(0, 'a'), (1, 'b'), (1, 'c'), (0, 'd'), (1, 'e'), (2, 'f'), (2, 'g'), (0, 'h'), (1, 'i')] >>> yx.sort() >>> yx [(0, 'a'), (0, 'd'), (0, 'h'), (1, 'b'), (1, 'c'), (1, 'e'), (1, 'i'), (2, 'f'), (2, 'g')] >>> x_sorted = [x for y, x in yx] >>> x_sorted ['a', 'd', 'h', 'b', 'c', 'e', 'i', 'f', 'g']
Combine these together to get:
[x for y, x in sorted(zip(Y, X))]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With