On Fri, Feb 5, 2010 at 8:48 AM, Zachary Pincus <zachary.pin...@yale.edu> 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. > > 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? >>
maybe np.in1d(u, u+1) or np.in1d(u,u+1).all() is what you want >>> help(np.in1d) Help on function in1d in module numpy.lib.arraysetops: in1d(ar1, ar2, assume_unique=False) Test whether each element of a 1D array is also present in a second array. Returns a boolean array the same length as `ar1` that is True where an element of `ar1` is in `ar2` and False otherwise. Josef _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion