numpy.lexsort#
- numpy.lexsort(keys,axis=-1)#
Perform an indirect stable sort using a sequence of keys.
Given multiple sorting keys, lexsort returns an array of integer indicesthat describes the sort order by multiple keys. The last key in thesequence is used for the primary sort order, ties are broken by thesecond-to-last key, and so on.
- Parameters:
- keys(k, m, n, …) array-like
Thek keys to be sorted. Thelast key (e.g, the lastrow ifkeys is a 2D array) is the primary sort key.Each element ofkeys along the zeroth axis must bean array-like object of the same shape.
- axisint, optional
Axis to be indirectly sorted. By default, sort over the last axisof each sequence. Separate slices alongaxis sorted overindependently; see last example.
- Returns:
- indices(m, n, …) ndarray of ints
Array of indices that sort the keys along the specified axis.
See also
argsortIndirect sort.
ndarray.sortIn-place sort.
sortReturn a sorted copy of an array.
Examples
Sort names: first by surname, then by name.
>>>importnumpyasnp>>>surnames=('Hertz','Galilei','Hertz')>>>first_names=('Heinrich','Galileo','Gustav')>>>ind=np.lexsort((first_names,surnames))>>>indarray([1, 2, 0])
>>>[surnames[i]+", "+first_names[i]foriinind]['Galilei, Galileo', 'Hertz, Gustav', 'Hertz, Heinrich']
Sort according to two numerical keys, first by elementsof
a, then breaking ties according to elements ofb:>>>a=[1,5,1,4,3,4,4]# First sequence>>>b=[9,4,0,4,0,2,1]# Second sequence>>>ind=np.lexsort((b,a))# Sort by `a`, then by `b`>>>indarray([2, 0, 4, 6, 5, 3, 1])>>>[(a[i],b[i])foriinind][(1, 0), (1, 9), (3, 0), (4, 1), (4, 2), (4, 4), (5, 4)]
Compare against
argsort, which would sort each key independently.>>>np.argsort((b,a),kind='stable')array([[2, 4, 6, 5, 1, 3, 0], [0, 2, 4, 3, 5, 6, 1]])
To sort lexicographically with
argsort, we would need to provide astructured array.>>>x=np.array([(ai,bi)forai,biinzip(a,b)],...dtype=np.dtype([('x',int),('y',int)]))>>>np.argsort(x)# or np.argsort(x, order=('x', 'y'))array([2, 0, 4, 6, 5, 3, 1])
The zeroth axis ofkeys always corresponds with the sequence of keys,so 2D arrays are treated just like other sequences of keys.
>>>arr=np.asarray([b,a])>>>ind2=np.lexsort(arr)>>>np.testing.assert_equal(ind2,ind)
Accordingly, theaxis parameter refers to an axis ofeach key, not ofthekeys argument itself. For instance, the array
arris treated asa sequence of two 1-D keys, so specifyingaxis=0is equivalent tousing the default axis,axis=-1.>>>np.testing.assert_equal(np.lexsort(arr,axis=0),...np.lexsort(arr,axis=-1))
For higher-dimensional arrays, the axis parameter begins to matter. Theresulting array has the same shape as each key, and the values are whatwe would expect if
lexsortwere performed on corresponding slicesof the keys independently. For instance,>>>x=[[1,2,3,4],...[4,3,2,1],...[2,1,4,3]]>>>y=[[2,2,1,1],...[1,2,1,2],...[1,1,2,1]]>>>np.lexsort((x,y),axis=1)array([[2, 3, 0, 1], [2, 0, 3, 1], [1, 0, 3, 2]])
Each row of the result is what we would expect if we were to perform
lexsorton the corresponding row of the keys:>>>foriinrange(3):...print(np.lexsort((x[i],y[i])))[2 3 0 1][2 0 3 1][1 0 3 2]