What is the numpy.msort() function from NumPy in Python?
Overview
The msort() function in Python is simply used to return a copy of an array sorted along the first axis.
Syntax
numpy.msort(a)
Parameter value
The msort() function takes a single and mandatory parameter value a, which represents the input array to be sorted.
Return array
The msort() function returns an array of the same type and shape as the input array a passed to it.
Let’s see how to use to it through the following code example.
Example
import numpy as np# creating lists of numbersmyarray = np.array([[3, 5, 2, 4, 1], [9, 6, 10, 8, 7]])# implementing the lexsort() function to sort mylist1 firstsortedarray = np.msort(myarray)print(sortedarray)
Code explanation
- Line 1: We import the
numpymodule. - Line 3: We create a
2Darray using thearray()function - Line 7: We implement the
msort()function on the arraymyarray. The result is assigned to a variablesortedarray. - Line 9: We print the newly sorted array variable,
sortedarray.