[Numpy-discussion] comparing container objects with arrays

Pearu Peterson pearu at scipy.org
Tue Feb 21 00:55:03 EST 2006


Question: what is the recommended way to compare two array objects? And 
when they are contained in a tuple or list or dictinary or etc.?

I ask because I found that arr1.__eq__(arr2) can return either bool or an 
array of bools when shape(arr1)!=shape(arr2) or shape(arr1)==shape(arr2), 

>>> array([1,2])==array([1,0,0])
>>> array([1,2])==array([1,0])
array([True, False], dtype=bool)

I wonder if numpy users are happy with that? Shouldn't arr1==arr2 return 
bool as well because current __eq__ behaviour is handled by equal() 
function when the shapes are equal?

Note that if __eq__ would always return bool then the following codes
would work as I would expect:

>>> (1,array([1,2]))==(1,array([1,2]))
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()

>>> # I would expect True, compare with
>>> (1,[1,2])==(1,[1,2])

>>> (1,array([1,2]))==(1,array([1,2,0]))

I started to write this message because

   object1 == object2

returns boolean for (all?) Python builtin objects but as soon
as these objects contain arrays, the test will fail with an exception.

May be numpy needs equalobjs(obj1,obj2) that always returns boolean
and can handle comparing objects like {1:array([1,2])}, [3,[array([2,2])],


More information about the NumPy-Discussion mailing list