numpy.sort in PythonIn some cases, we require a sorted array for computation. For this purpose, the numpy module of Python provides a function called numpy.sort(). This function gives a sorted copy of the source array or input array. Syntax:Parameters:x: array_like This parameter defines the source array, which is going to be sorted. axis: int or None(optional) This parameter defines the axis along which the sorting is performed. If this parameter is None, the array will be flattened before sorting, and by default, this parameter is set to -1, which sorts the array along the last axis. kind: {quicksort, heapsort, mergesort}(optional) This parameter is used to define the sorting algorithm, and by default, the sorting is performed using 'quicksort'. order: str or list of str(optional) When an array is defined with fields, its order defines the fields for making a comparison in first, second, etc. Only the single field can be specified as a string, and not necessarily for all fields. However, the unspecified fields will still be used, in the order in which they come up in the dtype, to break the ties. Returns:This function returns a sorted copy of the source array, which will have the same shape and type as a source array. Example 1:Output: array([[ 1, 4, 2, 3], [ 9, 13, 61, 1], [43, 24, 88, 22]]) array([[ 1, 2, 3, 4], [ 1, 9, 13, 61], [22, 24, 43, 88]]) In the above code
In the output, it shows a sorted copy of the source array of the same type and shape. Example 2:Output: array([[ 1, 4, 2, 3], [ 9, 13, 61, 1], [43, 24, 88, 22]]) array([ 1, 1, 2, 3, 4, 9, 13, 22, 24, 43, 61, 88]) Example 3:Output: array([[ 1, 4, 2, 1], [ 9, 13, 61, 3], [43, 24, 88, 22]]) array([[ 1, 2, 3, 4], [ 1, 9, 13, 61], [22, 24, 43, 88]]) Example 4:Output: array([('Shubham', 5.9, 23, 'M'), ('Arpita', 5.6, 23, 'F'), ('Vaishali', 5.2, 30, 'F')],dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'), ('gender', 'S10')]) array([('Arpita', 5.6, 23, 'F'), ('Shubham', 5.9, 23, 'M'), ('Vaishali', 5.2, 30, 'F')], dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'), ('gender', 'S10')]) array([('Arpita', 5.6, 23, 'F'), ('Shubham', 5.9, 23, 'M'), ('Vaishali', 5.2, 30, 'F')], dtype=[('name', 'S10'), ('height', '<f8'), ('age', '<i4'), ('gender', 'S10')]) In the above code
In the output, it shows a sorted copy of the structured array with a defined order. Next Topicnumpy.average() |