numpy array sorting weirdness

Daniel Fetchinson fetchinson at googlemail.com
Sat Mar 28 23:35:25 EDT 2009


>> 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.

Thanks a lot!

The only thing is that now I'll have to go through my whole code that
I adopted from pure python to numpy by simply replacing lists with
numpy.array. Maybe other stuff will cause problems too.

Is there any reason the 'axis' keyword argument doesn't default to the
value that corresponds to python list behaviour? That would make lot
of sense I think. Or retaining compatibility with python lists is not
really a goal of numpy.array?

Cheers,
Daniel



-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list