Python implements multi-dimensional array sorting

**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

Python implements multi-dimensional array sorting
Python implements ten classic sorting algorithms
python print array
Python implements Super Mario
Python implements tic-tac-toe game
Python implements tic-tac-toe game
python array add array_Python add array
Python implements man-machine gobang
Python implements Tetris game
Python implements minesweeper game
Python implements scanning tools
Python implements threshold regression
Python implements minesweeper games
Python implements electronic dictionary
Python implements guessing game
Python implements simple tank battle
Python implements udp chat window
Python implements WeChat airplane game
Python implements word guessing game
Python implements a guessing game
Python implements digital bomb game
Python implements TCP file transfer
Python numpy implements rolling case
Python implements simple tic-tac-toe game
Python implements password strength verification
Python implements code block folding
Python implements panoramic image stitching
Python implements SMTP mail sending
How Python implements FTP function
Python implements mean-shift clustering algorithm
Python implements verification code recognition
Python implements gradient descent method
Python implements text version minesweeper
Create Python two-dimensional array correctly
Python implements image stitching function
Python implements the brick-and-mortar game
Python implements student performance evaluation system
How Python implements the mail function
Python simply implements the snake game
Detailed sorting algorithm (implemented in Python)
Python3 implements the singleton design pattern
Python implements exchange rate conversion operations
Python implements string and number splicing
Python implements a universal web framework
Python implements 126 mailboxes to send mail
Python implements AI face change function
Python implements the steepest descent method
Python implements the actual banking system
Python implements digital bomb game program
Python implements ftp file transfer function
Python implements username and password verification
How Python implements the timer function
Python implements the aircraft war project
Python implements horizontal stitching of pictures
Python implements GIF graph upside down