[Numpy-discussion] Improving Complex Comparison/Ordering in Numpy

Stephan Hoyer shoyer at gmail.com
Wed Jul 1 15:48:57 EDT 2020


On Wed, Jul 1, 2020 at 12:23 PM Sebastian Berg <sebastian at sipsolutions.net>
wrote:

> This is a WIP, but allows nicely to try out how the new API
> could/should look like, and see the potential impact to code.  The
> current choice is for:
>
>     np.sort(arr, keys=(arr.real, arr.image))
>
> for example.  `keys` is like the `key` argument to pythons sorts, but
> unlike python sorts is not passed a function but rather a sequence of
> arrays.
>
> Alternative spellings could be `by=...`? Or maybe someone has a
> different API idea.
>

I really like the look of np.sort(arr, by=(arr.real, arr.image)).
- This avoids adding an extra function sortby into NumPy's API. The default
behavior (by=None) would of course be to sort by the arrays being sorted,
so it's backwards compatible.
- Calling the new argument "by" instead of "key" avoids confusion with the
behavior of Python's sort/sorted (which take functions instead of
sequences).

The combination of lexsort() and take_along_axis() makes it possible to
achieve this behavior currently, but it is definitely less clear than a
single function call.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20200701/2eea413c/attachment-0001.html>


More information about the NumPy-Discussion mailing list