[Numpy-discussion] three-way comparisons

Phillip Feldman phillip.m.feldman at gmail.com
Sat May 14 03:23:07 EDT 2016


I often find a need to do the type of comparison done by function shown
below.  I suspect that this would be more efficient for large arrays if
implemented direction in C.  Is there any possibility of adding something
like this to NumPy?

def three_way(x, y):
   """
   This function performs a 3-way comparison on `x` and `y`, which must be
   either lists or arrays of compatible shape.  Each pair of items or
elements--
   let's call them x[i] and y[i]--are compared.  The corresponding element
in
   the output array is 1 if `x[i]` is greater then `y[i]`, -1 of `x[i]` is
less,
   and zero if the two are equal.
   """
   numpy.greater(y, x).astype(int) - numpy.less(y, x).astype(int)

Phillip
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20160514/b2a986dc/attachment.html>


More information about the NumPy-Discussion mailing list