[SciPy-user] comparing two lists/arrays

nicky van foreest vanforeest at gmail.com
Wed Jul 15 13:59:51 EDT 2009


Hi,

Thanks for your advice.

Nicky

2009/7/15 Chris Colbert <sccolbert at gmail.com>:
> this should work for any case:
>
>>>> y = np.random.rand(5)
>>>> x = np.random.rand(5)
>
>>>> y
> array([ 0.21991179,  0.82874802,  0.65327351,  0.02277029,  0.14618527])
>
>>>> x
> array([ 0.46541554,  0.86719123,  0.50618409,  0.13140126,  0.24533278])
>
>>>> (y - x) > 0
> array([False, False,  True, False, False], dtype=bool)
>
>
> On Wed, Jul 15, 2009 at 8:53 AM, Robert Cimrman<cimrman3 at ntc.zcu.cz> wrote:
>> Scott Sinclair wrote:
>>>> 2009/7/15 Scott Sinclair <scott.sinclair.za at gmail.com>:
>>>>> 2009/7/15 Robert Cimrman <cimrman3 at 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
>>
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-user at scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



More information about the SciPy-User mailing list