1. 程式人生 > 其它 >numpy-np.lexsort的用法

numpy-np.lexsort的用法

技術標籤:numpy學習pythonnumpy

官方文件:https://numpy.org/doc/stable/reference/generated/numpy.lexsort.html

官方給出的說明是:

numpy.lexsort(keys, axis=-1)
Perform an indirect stable sort using a sequence of keys.

Given multiple sorting keys, which can be interpreted as columns in a spreadsheet, 
lexsort returns an array of integer indices that describes the sort order by multiple columns. 
The last key in the sequence is used for the primary sort order, the second-to-last key for the secondary sort order, and so on. 
The keys argument must be a sequence of objects that can be converted to arrays of the same shape. 
If a 2D array is provided for the keys argument, it’s rows are interpreted as the sorting keys and sorting is according to the last row, second last row etc.

Parameters
keys(k, N) array or tuple containing k (N,)-shaped sequences
The k different “columns” to be sorted. The last column (or row if keys is a 2D array) is the primary sort key.

axisint, optional
Axis to be indirectly sorted. By default, sort over the last axis.

Returns
indices(N,) ndarray of ints
Array of indices that sort the keys along the specified axis.

這種排序方式,是根據給定引數從最後一個引數開始進行排序,如果最後一個引數給定的值相同,則根據倒數第二個引數進行排序,以此類推。

例子:

1.對字母進行排序:

surnames = ('Hertz',    'Galilei', 'Hertz')
first_names = ('Heinrich', 'Galileo', 'Gustav')
ind = np.lexsort((first_names, surnames))
print(ind)

"""
輸出:
[1 2 0]
"""

首先按照surnames進行排序,字母G在H前面,'Galilei'排在第一位,剩餘兩個'Hertz'值相同,開始按照倒數第二個引數first_names進行排序,字母G在H前面,'Gustav'排在'Heinrich'前面,此時,兩個引數中的三個值完成排序:'Galilei','Hertz','Hertz',分別對應下標[1, 2, 0]。

list1 = [surnames[i] + ", " + first_names[i] for i in ind]
print(list1)

"""
輸出:
['Galilei, Galileo', 'Hertz, Gustav', 'Hertz, Heinrich']
"""

2.對數字進行排序:

a = [1,5,1,4,3,4,4] # First column
b = [9,4,0,4,0,2,1] # Second column
ind = np.lexsort((b,a)) # Sort by a, then by b
print(ind)
print([(a[i],b[i]) for i in ind])

"""
輸出:
[2 0 4 6 5 3 1]
[(1, 0), (1, 9), (3, 0), (4, 1), (4, 2), (4, 4), (5, 4)]
"""

首先按照a進行排序,a中值相同則按照b進行排序。

3.對array進行排序:

x = np.array([
    [2, 1, 5, 1, 7],
    [3, 7, 6, 5, 9],
    [1, 3, 3, 4, 10]
])
print(x)
ind = np.lexsort(x)
print(ind)

"""
輸出:
[[ 2  1  5  1  7]
 [ 3  7  6  5  9]
 [ 1  3  3  4 10]]
[0 2 1 3 4]
"""

對array排序,是按照最後一行開始,一次倒數第二行、倒數第三行、以此類推的順序進行排序。

感謝參考文章中的一段話:

參考:

https://charlesnord.github.io/2017/04/16/numpy-lexsort/