Sorting and Unique
Argsort
Produce the index ordering that would sort a 6-element list. The naive version
builds (value, index) pairs, sorts them, then extracts the indices — keeping
the sort structure visible without a nested loop. The trace shows pairs
growing, then reordering in one step, then order filling with the extracted
indices.
By hand
Build a list of [value, index] pairs. Sort the pairs (Python sorts lists
lexicographically, so they sort by value then by index). Extract the second
element of each sorted pair into order.
naive.py
values = [3, 1, 4, 2, 5, 0]
pairs = []
for i in range(len(values)):
pairs.append([values[i], i])
pairs.sort()
order = []
for p in pairs:
order.append(p[1])
print('RESULT:', order)
With NumPy
np.argsort(a) returns an array of integer indices such that a[indices]
gives a in ascending order.
library.py
import numpy as np
values = [3, 1, 4, 2, 5, 0]
a = np.array(values)
result = np.argsort(a)
print('a: shape:', a.shape, 'dtype:', a.dtype, 'values:', a.tolist())
print('result: shape:', result.shape, 'dtype:', result.dtype, 'values:', result.tolist())
print('RESULT:', result.tolist())
a: shape: (6,) dtype: int64 values: [3, 1, 4, 2, 5, 0]
result: shape: (6,) dtype: int64 values: [5, 1, 3, 0, 2, 4]
RESULT: [5, 1, 3, 0, 2, 4]
Implementation notes
- Verify:
a[[5, 1, 3, 0, 2, 4]]=[0, 1, 2, 3, 4, 5]— applying the argsort indices toayields the sorted values (seefancy-indexfor index-array selection). - Argsort is useful to reorder a parallel array: if
namesandscoresshare the same positional index,argsort(scores)gives the index order to sortnamesby score without losing alignment. - For the rank-assignment pattern (each element gets its sorted rank) see
rank-assignin the python-data-basics book. - Shape, dtype, and values are shown explicitly here because
ndarray.__repr__output varies with NumPy version and print options.