numpy array sorting weirdness

John O'Hagan research at johnohagan.com
Sun Mar 29 04:50:51 CEST 2009


On Sun, 29 Mar 2009, Daniel Fetchinson wrote:

[...]

> The fact that the following two outputs are not the same is a bug or a
> feature of numpy?
>
> ##### I would have thought the two array outputs would be the same ######
>
> import numpy
>
> a = [ [ 0, 0 ], [ 1, 0 ], [ 1, 1 ] ]
>
> pythonarray = a
> pythonarray.sort( )
> print pythonarray
>
> numpyarray = numpy.array( a )
> numpyarray.sort( )
> print numpyarray
[...]

There's some numpy documentation here:

http://www.tramy.us/numpybook.pdf

The numpy array sort method takes an optional keyword argument "axis", 
enabling sorting along any axis of an array, and which defaults to -1. 

In your case that means it sorts each sublist. If you use a.sort(axis=0), the 
main list will be sorted, like a normal python sort.

Regards,

John



More information about the Python-list mailing list