[SciPy-user] comparing two lists/arrays
Robert Cimrman
cimrman3@ntc.zcu...
Wed Jul 15 07:53:57 CDT 2009
Scott Sinclair wrote:
>> 2009/7/15 Scott Sinclair <scott.sinclair.za@gmail.com>:
>>> 2009/7/15 Robert Cimrman <cimrman3@ntc.zcu.cz>:
>>> nicky van foreest wrote:
>>>> Given two vectors x and y, the (perhaps) common mathematical
>>>> definition of x < y is that x_i < y_i for all i. Thus, the
>>>> mathematical comparison x <y returns just one boolean, not an array of
>>>> booleans for each x_i < y_i. I implemented this behavior as
>>>> prod(less(X,Y)) (I use less to be able to deal with lists X and Y
>>>> also). Is there perhaps a more straighforward/elegant/readible way to
>>>> achieve the same behavior?
>>> assuming x, y are numpy arrays: (x < y).all()
>> You could do the following to handle the case where they aren't:
>>
>>>>> import numpy as np
>>>>> x = range(10)
>>>>> y = range(1, 11)
>>>>> np.all(x < y)
>> True
>
> Scratch that
>
>>>> x < y
> True
beware!
y[2] = -1
In [21]: y
Out[21]: [1, 2, -1, 4, 5, 6, 7, 8, 9, 10]
In [22]: x < y
Out[22]: True
More information about the SciPy-user
mailing list