[SciPy-User] all() and any() with array-like input

Keith Goodman kwgoodman@gmail....
Sun Jan 31 23:07:36 CST 2010


On Sun, Jan 31, 2010 at 8:39 PM, Skipper Seabold <jsseabold@gmail.com> wrote:
> I'm sure this has come up before, but it's difficult to search of any
> and all...  This is tripping me up, but maybe there is just something
> I am missing.
>
> In [1]: import numpy as np
>
> In [2]: X = [.2,.2,.2,.2,.2]
>
> In [3]: np.all(X <= 1)
> Out[3]: False
>
> In [4]: np.all(X >= 0)
> Out[4]: True
>
> In [5]: np.all(np.asarray(X) <= 1)
> Out[5]: True
>
> In [6]: np.any(X>1)
> Out[6]: True
>
> I guess it's simple enough to use asarray, but I was just curious what
> drives this behavior since the docs indicate that it should work with
> array-like structures.

"X <= 1", where X is a list, is not array-like, it is a bool:

>>> X <= 1
False

So I guess it is a python thing. These are weird:

>>> [] <= 1
False
>>> {} < []
True
>>> {} > []
False


More information about the SciPy-User mailing list