numpy sorts according to a certain column

numpy.lexsort()Used to sort multiple sequences. Think of it as sorting a spreadsheet, each column represents a sequence, and priority is given to the lower columns when sorting.

  • The original array is not modified, and the index is returned.

Sort according to the first column, such as:

import numpy as np

a = [[100, 2, 34], [12, 45, 2], [45, 90, 21]]
a = np.array(a)

b = a[:, 0] # [100  12  45]
index = np.lexsort((b,)) # [1 2 0]
print(a[index]) 

result:

[[ 12  45   2]
 [ 45  90  21]
 [100   2  34]]

Sort according to the first line, such as:

import numpy as np

a = [[100, 2, 34], [12, 45, 2], [45, 90, 21]]
a = np.array(a)

b = a[0, :] # [100   2  34]
index = np.lexsort((b,)) # [1 2 0]
print(a.T[index].T)

result:

[[  2  34 100]
 [ 45   2  12]
 [ 90  21  45]]

Reference link: here1 , here2

Guess you like

Origin blog.csdn.net/qq_26460841/article/details/114582319