comparing two arrays

Robert Kern robert.kern at gmail.com
Mon Jun 19 11:59:58 EDT 2006


Bas wrote:
> You are comparing a normal python list to a constant, which are
> obviously unequal. Try converting your lists to arrays first
> (untested):
> 
> import numeric/numpy as N
> a =N.array([0,1,2,5,6,6])
> b = N.array([5,4,1,6,4,6])
> print a==6 and b==6
> print N.where(a==6 and b==6)

Careful there. The "and" keyword cannot be overloaded and so neither Numeric nor
numpy does. Either N.logical_and() should be used or (since the results of a==6
and b==6 are known to be boolean arrays) the & operator works fine as well.


In [9]: import numpy as np

In [10]: a = np.array([0,1,2,5,6,6])

In [11]: b = np.array([5,4,1,6,4,6])

In [12]: (a==6) & (b==6)
Out[12]: array([False, False, False, False, False, True], dtype=bool)

In [13]: np.where((a==6) & (b==6))
Out[13]: (array([5]),)


The OP may also find that numpy questions are best handled on numpy-discussion
rather than comp.lang.python .

  https://lists.sourceforge.net/lists/listinfo/numpy-discussion

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list