**On how to compound multi-dimensional arrays **
Such as array:
import numpy as np
data = np.array([[2,2,5],[2,1,3],[1,2,3],[3,1,4]])
data
array([[2,2,5],[2,1,3],[1,2,3],[3,1,4]])
Sort the array in ascending order of the first column, ascending order of the second column, and ascending order of the third column:
idex=np.lexsort([data[:,2], data[:,1], data[:,0]])
sorted_data = data[idex,:]
sorted_data
array([[1,2,3],[2,1,3],[2,2,5],[3,1,4]])
Then sort the array in descending order of the first column, ascending order of the second column, and ascending order of the third column:
idex=np.lexsort([data[:,2], data[:,1],-1*data[:,0]])
sorted_data = data[idex,:]
sorted_data
array([[3,1,4],[2,1,3],[2,2,5],[1,2,3]])
Supplementary extension: python: descending order of multidimensional arrays
In python, unfortunately, there is no way to sort the multidimensional array in descending order according to the specified dimension.
However, there is a descending order of the one-dimensional array, which is enough!
# Author: Right.Q
# Realize the reverse order arrangement of multi-dimensional matrix
def descend_sort(array):'''Sort three-dimensional arrays in reverse order'''[height, width, channel]= array.shape
sortArray = np.zeros([height, width, channel])for h inrange(height):for w inrange(width):
sortArray[h, w,:]=sorted(array[h, w,:], reverse=True)return sortArray
The formal parameter is a specified three-dimensional array. If there are more dimensions, the dimensions can be automatically identified.
The above python implementation of multi-dimensional array sorting is all the content shared by the editor. I hope to give you a reference.
Recommended Posts