This is an archival dump of old wiki content --- see scipy.org for current material

Abstract

Perform an indirect sort using a list of keys. The first key is sorted, then the second, and so on through the list of keys. At each step the previous order is preserved when equal keys are encountered. The result is a sort on multiple keys. If the keys represented columns of a spreadsheet, for example, this would sort using multiple columns (the last key being used for the primary sort order, the second-to-last key for the secondary sort order, and so on). The keys argument must be a sequence of things that can be converted to arrays of the same shape.

Signature


Function
lexsort(keys=, axis=-1)


Arguments

return
An array of indices with the same shape as the keys.
keys
a tuple of array objects.
axis
Axis to be indirectly sorted. None indicates that the flattened array should be used. Default is -1.

Details

xxx

Examples

   1 >>> from numpy import *
   2 >>> serialnr = array([1023, 5202, 6230, 1671, 1682, 5241])
   3 >>> height = array([40., 42., 60., 60., 98., 40.])
   4 >>> width = array([50., 20., 70., 60.,  15., 30.])
   5 >>>
   6 >>> # We want to sort the serial numbers with increasing height, _AND_
   7 >>> # serial numbers with equal heights should be sorted with increasing width.
   8 >>>
   9 >>> indices = lexsort(keys = (width, height))        # mind the order!
  10 >>> indices
  11 array([5, 0, 1, 3, 2, 4])
  12 >>> for n in indices:
  13 ...   print serialnr[n], height[n], width[n]
  14 ...
  15 5241 40.0 30.0
  16 1023 40.0 50.0
  17 5202 42.0 20.0
  18 1671 60.0 60.0
  19 6230 60.0 70.0
  20 1682 98.0 15.0
  21 >>>
  22 >>> a = vstack([serialnr,width,height])                              # Alternatively: all data in one big matrix
  23 >>> print a                                                          # Mind the order of the rows!
  24 [[ 1023.  5202.  6230.  1671.  1682.  5241.]
  25 [   50.    20.    70.    60.    15.    30.]
  26 [   40.    42.    60.    60.    98.    40.]]
  27 >>> indices = lexsort(a)                                             # Sort on last row, then on 2nd last row, etc.
  28 >>> a.take(indices, axis=-1)
  29 array([[ 5241.,  1023.,  5202.,  1671.,  6230.,  1682.],
  30 [   30.,    50.,    20.,    60.,    70.,    15.],
  31 [   40.,    40.,    42.,    60.,    60.,    98.]])

Notes

xxx

See Also

argsort : the sort is returned as an array of indices.

sort : returns a sorted copy or done inplace.

SciPy: lexsort (last edited 2015-10-24 17:48:25 by anonymous)