Sorting Multidimesional array(newbie)

Robert Kern robert.kern at gmail.com
Tue Dec 12 09:00:02 EST 2006


Fredrik Lundh wrote:
> also note the OP didn't specify what to do for records where the first 
> column was identical, so I guess a plain sort() call, without any custom 
> compares or mappings, would work as well as the fancier alternatives...

If the OP had lists of lists, yes. However, he seems to be using one of (numpy,
numarray, Numeric). Because of rich comparisons, just using sorted() won't work.
The cheap and cheerful approach would be to convert to lists of lists using
.tolist(), .sort() the list, and then reconstruct the array object. Depending on
the size of the array, that may be just fine.

numpy (and maybe numarray, I'd have to check) has a function lexsort(), which
unfortunately has a confusing interface.


In [2]: lexsort?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
Namespace:      Interactive
Docstring:
    lexsort(keys=, axis=-1) -> array of indices. argsort with list of keys.

    Return an array of indices similar to argsort, except the sorting is
    done using the provided sorting keys.  First the sort is done using
    key[0], then the resulting list of indices is further manipulated by
    sorting on key[1], and so forth. 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.


This is the idiomatic way to lexicographically sort an array by columns
equivalent to the .tolist() approach I give above. I usually wrap lexsort() with
these operations so my brain doesn't explode.


In [15]: from numpy import *

In [16]: a = array([[5, 2], [1, 3], [1, 2]])

In [17]: a[lexsort(keys=a.transpose()[::-1])]
Out[17]:
array([[1, 2],
       [1, 3],
       [5, 2]])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list