[Numpy-discussion] u in [u+1]
Zachary Pincus
zachary.pincus@yale....
Fri Feb 5 07:48:35 CST 2010
> I'm having some trouble here. I have a list of numpy arrays. I
> want to
> know if an array 'u' is in the list.
Try:
any(numpy.all(u == l) for l in array_list)
standard caveats about float comparisons apply; perhaps
any(numpy.allclose(u, l) for l in array_list)
is more appropriate in certain circumstances.
Can of course replace the first 'any' with 'all' or 'sum' to get
different kinds of information, but using 'any' is equivalent to the
'in' query that you wanted.
Why the 'in' operator below fails is that behind the scenes, 'u not in
[u+1]' causes Python to iterate through the list testing each element
for equality with u. Except that as the error states, arrays don't
support testing for equality because such tests are ambiguous. (cf.
many threads about this.)
Zach
On Feb 5, 2010, at 6:47 AM, Neal Becker wrote:
> I'm having some trouble here. I have a list of numpy arrays. I
> want to
> know if an array 'u' is in the list.
>
> As an example,
> u = np.arange(10)
>
> : u not in [u+1]
> ---------------------------------------------------------------------------
> ValueError Traceback (most recent
> call last)
>
> /home/nbecker/raysat/test/<ipython console> in <module>()
>
> ValueError: The truth value of an array with more than one element is
> ambiguous. Use a.any() or a.all()
>
> What would be the way to do this?
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
More information about the NumPy-Discussion
mailing list