[Numpy-discussion] why sort does not accept a key?

Charles R Harris charlesr.harris at gmail.com
Mon Mar 24 13:41:12 EDT 2014


On Mon, Mar 24, 2014 at 11:05 AM, Alan G Isaac <alan.isaac at gmail.com> wrote:

> > On Mon, Mar 24, 2014 at 12:08 PM, Alan G Isaac
> >> what is the preferred idiom for a descending sort?
>
>
> On 3/24/2014 12:13 PM, josef.pktd at gmail.com wrote:
> > adding [::-1] just creates a new view, pretty low cost.
>
>
> I meant when you need to sort on a key (another vector).
>
> Currently I'm just reversing the result of argsort with [::-1]
> but this changes the sort order (relative to a stable descending sort).
>
>
For integer types you can use the complement as the key

In [9]: ~arange(4, dtype=uint8)
Out[9]: array([255, 254, 253, 252], dtype=uint8)

In [10]: ~arange(4, dtype=int8)
Out[10]: array([-1, -2, -3, -4], dtype=int8)

For float types you would need to use the negative.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140324/8f554907/attachment.html>


More information about the NumPy-Discussion mailing list