


How to sort a two-dimensional array according to a row or column in Python
This article mainly introduces Python's method of sorting two-dimensional arrays according to a certain row or column. It analyzes the common operating techniques of Python using the lexsort method of the numpy module to sort two-dimensional arrays based on specific examples. Friends who need it You can refer to the following
The example of this article describes how Python implements the method of sorting a two-dimensional array according to a certain row or column. Share it with everyone for your reference, the details are as follows:
lexsort supports sorting arrays in the order of specified rows or columns; it is indirect sorting, lexsort does not modify the original array and returns the index.
(Corresponding to lexsort one-dimensional array is argsort a.argsort()
You can use it this way; argsort does not modify the original array and returns the index)
The default is to press the last The elements in a row are sorted from small to large, and the index position of the last row of elements after sorting is returned.
Suppose array a, the returned index ind, ind returns a one-dimensional array
For a one-dimensional array, a[ind] is the sorted array.
For two-dimensional arrays, detailed examples will be given below.
import numpy as np >>> a array([[ 2, 7, 4, 2], [35, 9, 1, 5], [22, 12, 3, 2]])
Sort by last column order
>>> a[np.lexsort(a.T)] array([[22, 12, 3, 2], [ 2, 7, 4, 2], [35, 9, 1, 5]])
Sort by last column in reverse order
>>>a[np.lexsort(-a.T)] array([[35, 9, 1, 5], [ 2, 7, 4, 2], [22, 12, 3, 2]])
By first column Sort in order
>>> a[np.lexsort(a[:,::-1].T)] array([[ 2, 7, 4, 2], [22, 12, 3, 2], [35, 9, 1, 5]])
Sort in order of the last row
>>> a.T[np.lexsort(a)].T array([[ 2, 4, 7, 2], [ 5, 1, 9, 35], [ 2, 3, 12, 22]])
Sort in order of the first row
>>> a.T[np.lexsort(a[::-1,:])].T array([[ 2, 2, 4, 7], [ 5, 35, 1, 9], [ 2, 22, 3, 12]])
The above is the detailed content of How to sort a two-dimensional array according to a row or column in Python. For more information, please follow other related articles on the PHP Chinese website!

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond

Pythonisnotpurelyinterpreted;itusesahybridapproachofbytecodecompilationandruntimeinterpretation.1)Pythoncompilessourcecodeintobytecode,whichisthenexecutedbythePythonVirtualMachine(PVM).2)Thisprocessallowsforrapiddevelopmentbutcanimpactperformance,req

ToconcatenatelistsinPythonwiththesameelements,use:1)the operatortokeepduplicates,2)asettoremoveduplicates,or3)listcomprehensionforcontroloverduplicates,eachmethodhasdifferentperformanceandorderimplications.

Pythonisaninterpretedlanguage,offeringeaseofuseandflexibilitybutfacingperformancelimitationsincriticalapplications.1)InterpretedlanguageslikePythonexecuteline-by-line,allowingimmediatefeedbackandrapidprototyping.2)CompiledlanguageslikeC/C transformt

Useforloopswhenthenumberofiterationsisknowninadvance,andwhileloopswheniterationsdependonacondition.1)Forloopsareidealforsequenceslikelistsorranges.2)Whileloopssuitscenarioswheretheloopcontinuesuntilaspecificconditionismet,usefulforuserinputsoralgorit


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
