numpy.ndarray.partition#

method

ndarray.partition(kth,axis=-1,kind='introselect',order=None)#

Partially sorts the elements in the array in such a way that the value ofthe element in k-th position is in the position it would be in a sortedarray. In the output array, all elements smaller than the k-th elementare located to the left of this element and all equal or greater arelocated to its right. The ordering of the elements in the two partitionson the either side of the k-th element in the output array is undefined.

Parameters:
kthint or sequence of ints

Element index to partition by. The kth element value will be in itsfinal sorted position and all smaller elements will be moved before itand all equal or greater elements behind it.The order of all elements in the partitions is undefined.If provided with a sequence of kth it will partition all elementsindexed by kth of them into their sorted position at once.

Deprecated since version 1.22.0:Passing booleans as index is deprecated.

axisint, optional

Axis along which to sort. Default is -1, which means sort along thelast axis.

kind{‘introselect’}, optional

Selection algorithm. Default is ‘introselect’.

orderstr or list of str, optional

Whena is an array with fields defined, this argument specifieswhich fields to compare first, second, etc. A single field canbe specified as a string, and not all fields need to be specified,but unspecified fields will still be used, in the order in whichthey come up in the dtype, to break ties.

See also

numpy.partition

Return a partitioned copy of an array.

argpartition

Indirect partition.

sort

Full sort.

Notes

Seenp.partition for notes on the different algorithms.

Examples

>>>importnumpyasnp>>>a=np.array([3,4,2,1])>>>a.partition(3)>>>aarray([2, 1, 3, 4]) # may vary
>>>a.partition((1,3))>>>aarray([1, 2, 3, 4])
On this page