Skip to content Skip to sidebar Skip to footer

Sort A List Of Entries According To A Given Column And Return The List Of Indices

Given a list of entries (e.g. pieces of data, which are tuples), how to sort the list according to one column (feature, e.g. an int) and return not the entire sorted list of entrie

Solution 1:

import pandas as pd

lst = [13,6,3,2,1,7,6,8]
othr = [5,2,7,9,2,5,7,10]
df = pd.DataFrame({"list1": lst, "list2": othr})
result = df.sort_values("list1")

here ldf contain two lists, it is sorted by list1 and you can find the indices from index (result.index) of the dataframe.


Solution 2:

Not a short answer but if you must

alist = [(1,"b"),(5,"a"),(3,"c")]
index = {}
for i,item in enumerate(alist):
    index[item] = i

original_indexes = [index[x] for x in sorted(alist,key=lambda x: x[0])]

Solution 3:

my variation:

def sort_index(z):
  """
  >>> sort_index([(1,"b"),(5,"a"),(3,"c")])
  [0, 2, 1]
  """
  number = [a[0] for a in z]
  return [x[1] for y in number for x in zip(sorted(number), range(len(z))) if x[0] == y]

Solution 4:

In the dark ages it was usual to DSU (Decorate Sort Undecorate) to sort a list of objects according to an arbitrary attribute.

We can revert this pattern, now that we have the key argument, to keep the decoration only...

def argsort(l, field_no):
    return (t[0] for t in sorted(enumerate(l), key=lambda x:x[1] [field_no]))

Here the decoration is produced by the usual enumerate, that gives us the index of each item, so we have to sort a list of 2-tuples, the first element being the index and the 2nd the element of the original list, we use the key argument to sort according to a field of the original list, and we trow away the original list element...

In the following, a brief demo of the said approach

In [1]: from random import shuffle

In [2]: l = [(chr(60+i), i) for i in range(10)]

In [3]: shuffle(l); l
Out[3]: 
[('@', 4),
 ('?', 3),
 ('A', 5),
 ('<', 0),
 ('>', 2),
 ('C', 7),
 ('E', 9),
 ('B', 6),
 ('=', 1),
 ('D', 8)]

In [4]: def argsort(l, field_no):
   ...:     return (t[0] for t in sorted(enumerate(l), key=lambda x:x[1][field_no]))
   ...: 

In [5]: for i in argsort(l, 1): print(l[i])
('<', 0)
('=', 1)
('>', 2)
('?', 3)
('@', 4)
('A', 5)
('B', 6)
('C', 7)
('D', 8)
('E', 9)

In [6]: 

Note that here argsort returns a generator, change return (..) to return [...] if you need a list.


Post a Comment for "Sort A List Of Entries According To A Given Column And Return The List Of Indices"