numpy sort (sort, argsort, lexort, partition, sorted)

1.nunpy.sort

Here is a brief introduction of this method on the official website

numpy.sort(a, axis=1, kind='quicksort', order=None)
Parameters:	a : array_like
				Array to be sorted.
			axis : int or None, optional
				Axis along which to sort. If None, the array is flattened 
				before sorting. The default is 1, which sorts along the last
				 axis.
			kind : {'quicksort', 'mergesort', 'heapsort'}, optional
				Sorting algorithm. Default is 'quicksort'.
			order : str or list of str, optional
				When a is an array with fields defined, this argument
				specifies which fields to compare first, second, etc. A 
				single field can be specified as a string, and not all 
				fields need be specified, but unspecified fields will still 
				be used, in the order in which they come up in the dtype, to 
				break ties.
Returns:	sorted_array : ndarray
			Array of the same type and shape as a.

Illustrate with examples

Parameter order

import numpy as np
>>> dtype = [('Name', 'S10'), ('Height', float), ('Age', int)]
>>> values = [('Li', 1.8, 41), ('Wang', 1.9, 38),('Duan', 1.7, 38)]
>>> a = np.array(values, dtype=dtype)
>>> np.sort(a, order='Height')  # Sort by the attribute Height, and the parameter is a string                      
array([('Duan', 1.7, 38), ('Li', 1.8, 41),('Wang', 1.9, 38)],
      dtype=[('Name', '|S10'), ('Height', '<f8'), ('Age', '<i4')])
>>> np.sort(a, order=['Age', 'Height']) 
# Sort by attribute Age first. If Age is equal, then sort by Height. The parameter is list        
array([('Duan', 1.7, 38), ('Wang', 1.9, 38),('Li', 1.8, 41)],
      dtype=[('Name', '|S10'), ('Height', '<f8'), ('Age', '<i4')])

2.numpy. argsort

numpy.argsort(a, axis=1, kind='quicksort', order=None)
Parameters:	a : array_like
				Array to sort.
			axis : int or None, optional
				Axis along which to sort. The default is 1 (the last axis).
				If None, the flattened array is used.
			kind : {'quicksort', 'mergesort', 'heapsort'}, optional
				Sorting algorithm.
			order : str or list of str, optional
				When a is an array with fields defined, this argument
				specifies which fields to compare first, second, etc. A
				single field can be specified as a string, and not all fields
				need be specified, but unspecified fields will still be used,
				in the order in which they come up in the dtype, to break
				ties.
Returns:	index_array : ndarray, int
				Array of indices that sort a along the specified axis. If a
				is one-dimensional, a[index_array] yields a sorted a.

3.nupy.lexsort

numpy.argsort(a, axis=-1, kind='quicksort', order=None)
Parameters:		a : array_like
					Array to sort.
				axis : int or None, optional
					Axis along which to sort. The default is -1 (the last
					axis). If None, the flattened array is used.
				kind : {'quicksort', 'mergesort', 'heapsort'}, optional
					Sorting algorithm.
				order : str or list of str, optional
					When a is an array with fields defined, this argument
					specifies which fields to compare first, second, etc. A
					single field can be specified as a string, and not all 
					fields need be specified, but unspecified fields will 
					still be used, in the order in which they come up in the 
					dtype, to break ties.
Returns:		index_array : ndarray, int
					Array of indices that sort a along the specified axis. If
					a is one-dimensional, a[index_array] yields a sorted a

4.numpy.searchsorted

numpy.searchsorted(a, v, side='left', sorter=None)
Parameters:		a : 1-D array_like
					Input array. If sorter is None, then it must be sorted in
					ascending order, otherwise sorter must be an array of
					indices that sort it.
				v : array_like
					Values to insert into a.
				side : {'left', 'right'}, optional
					If 'left', the index of the first suitable location found
					is given. If 'right', return the last such index. If 
					there is no suitable index, return either 0 or N (where N 
					is the length of a).
				sorter : 1-D array_like, optional
					Optional array of integer indices that sort array a into
					ascending order. They are typically the result of 
					argsort.
Returns:		indices : array of ints
					Array of insertion points with the same shape as v

5.numpy.partition

numpy.partition(a, kth, axis=-1, kind='introselect', order=None)
Parameters:		a : array_like
					Array to be sorted.
				kth : int or sequence of ints
					Element index to partition by. The k-th value of the
					element will be in its final sorted position and all 
					smaller elements will be moved before it and all equal or 
					greater elements behind it. The order all elements in the 
					partitions is undefined. If provided with a sequence of 
					k-th it will partition all elements indexed by k-th of 
					them into their sorted position at once.
				axis : int or None, optional
					Axis along which to sort. If None, the array is flattened
					before sorting. The default is -1, which sorts along the 
					last axis.
				kind : {'introselect'}, optional
					Selection algorithm. Default is 'introselect'.
				order : str or list of str, optional
					When a is an array with fields defined, this argument
					specifies which fields to compare first, second, etc. A 
					single field can be specified as a string. Not all fields 
					need be specified, but unspecified fields will still be 
					used, in the order in which they come up in the dtype, to 
					break ties.
Returns:		partitioned_array : ndarray
					Array of the same type and shape as a

6.numpy.sorted

sorted(iterable[, cmp[, key[, reverse]]])
The sorted() function sorts all the objects that can be iterated.
	The difference between sort and sorted:
	Sort is a method applied to list. sorted can sort all objects that can be iterated.
	The sort method of list returns to operate on the existing list, while the sorted method of the built-in function returns a
	A new list, not an operation based on the original one.
#sorted() can reverse sort with the parameter reverse=True
>>>list=[3,4,2,6,1]
>>>sorted(list)
[1, 2, 3, 4, 6]
>>>sorted(list, reverse=True)
[6, 4, 3, 2, 1]

Published 10 original articles, won praise 1, visited 551
Private letter follow

Keywords: Attribute

Added by Rayne on Tue, 04 Feb 2020 20:30:33 +0200