![]() |
VOOZH | about |
Given two lists of equal length, where the second list defines the order, the task is to reorder the first list according to the sorted order of the second list.
Example:
Input:
List A (to sort): ['x', 'y', 'z', 'w']
List B (order list): [40, 10, 30, 20]Output:
['y', 'w', 'z', 'x']
Explanation: List B tells us the order in which to arrange the elements of list A. The smallest value in B is 10, which corresponds to 'y' in A, next is 20 -> 'w', then 30 -> 'z', and finally 40 -> 'x'. So after sorting A based on B, we get ['y', 'w', 'z', 'x'].
sorted()This method ties elements of both lists together and sorts them using the values from the second list.
['b', 'd', 'a', 'c']
Explanation:
()numpy.argsort() gives the index positions that would sort a list. Using these indices, we can reorder another list accordingly.
['b', 'd', 'a', 'c']
Explanation:
Pandas sorts one list by another easily by putting both into a DataFrame and sorting by the second list.
['b', 'd', 'a', 'c']
Explanation:
This method sorts the first list by directly using values from the second list as the sorting key.
['n', 'p', 'o', 'm']
Explanation: