+5 votes
in Programming Languages by (77.2k points)

I want to sort three lists based on one of those three lists. The first list will be sorted in ascending order, while others will change the positions of their corresponding elements based on the new indices of the elements in the first list. i.e. if a[3] becomes a[1], b[3] and c[3] will also become b[1] and c[1], respectively.

E.g.

a=[9,7,3,4,10,7,6,2,8]

b=[29,17,3,14,110,47,86,12,18]

c=[109,732,30,4,110,760,689,210,80]

to

a=[2, 3, 4, 6, 7, 7, 8, 9, 10]

b=[12, 3, 14, 86, 17, 47, 18, 29, 110]

c=[210, 30, 4, 689, 732, 760, 80, 109, 110]

Here, list "a" is sorted in ascending order, whereas lists "b" and "c" have corresponding elements placed as per the new indices of elements in the list "a."

1 Answer

+2 votes
by (354k points)
selected by
 
Best answer

You have first to zip your lists. The list that needs to be sorted in ascending order should be kept as the first parameter in the zip(). Use the sorted() function on the zipped element and then unzip it to get your lists as tuples.

If you want the list as output, you can convert the tuple to the list.

Here are examples to show how to sort multiple lists based on one list.

>>> a=[9,7,3,4,10,7,6,2,8]
>>> b=[29,17,3,14,110,47,86,12,18]
>>> c=[109,732,30,4,110,760,689,210,80]

>>> a,b,c=[list(v) for v in zip(*sorted(zip(a,b,c)))]
>>> a
[2, 3, 4, 6, 7, 7, 8, 9, 10]
>>> b
[12, 3, 14, 86, 17, 47, 18, 29, 110]
>>> c
[210, 30, 4, 689, 732, 760, 80, 109, 110]

>>> a,b,c=map(list, zip(*sorted(zip(a,b,c))))
>>> a
[2, 3, 4, 6, 7, 7, 8, 9, 10]
>>> b
[12, 3, 14, 86, 17, 47, 18, 29, 110]
>>> c
[210, 30, 4, 689, 732, 760, 80, 109, 110]
>>>

You can also use the argsort() function of the Numpy module to get the indices for sorting a list. You can then use those indices to sort all other lists.

Here is an example:

>>> import numpy as np
>>> a=[9,7,3,4,10,7,6,2,8]
>>> b=[29,17,3,14,110,47,86,12,18]
>>> c=[109,732,30,4,110,760,689,210,80]
>>> i=np.argsort(a)
>>> i
array([7, 2, 3, 6, 1, 5, 8, 0, 4])

>>> a,b,c=np.asarray(a)[i], np.asarray(b)[i], np.asarray(c)[i]
>>> a
array([ 2,  3,  4,  6,  7,  7,  8,  9, 10])
>>> b
array([ 12,   3,  14,  86,  17,  47,  18,  29, 110])
>>> c
array([210,  30,   4, 689, 732, 760,  80, 109, 110])
>>>


...