[Numpy-discussion] a==b for numpy arrays

David Huard david.huard at gmail.com
Mon Dec 11 13:18:18 EST 2006


Hi Daniel,

Just out of curiosity, what's wrong with
if all(a==b):
   ...

?


Cheers,

David

2006/12/11, Abel Daniel <abli at freemail.hu>:
>
> >
> Hi!
>
> My unittests got broken because 'a==b' for numpy arrays returns an
> array instead of returning True or False:
>
> >>> import numpy
> >>> a = numpy.array([1, 2])
> >>> b = numpy.array([1, 4])
> >>> a==b
> array([True, False], dtype=bool)
>
> This means, for example:
> >>> if a==b:
> ...   print 'equal'
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> ValueError: The truth value of an array with more than one element is
> ambiguous.
> Use a.any() or a.all()
> >>>
>
>
> Now, I think that having a way of getting an element-wise comparison
> (i.e. getting an array of bools) is great. _But_ why make that the
> result of a '==' comparison? Is there any actual code that does, for
> example
> >>> result_array = a==b
> or any variant thereof?
>
> Thanks in advance,
> Daniel
>
>
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20061211/e15bf91e/attachment.html>


More information about the NumPy-Discussion mailing list