difference between 2 arrays

Robert Kern robert.kern at gmail.com
Wed Aug 19 18:17:16 CEST 2009

On 2009-08-19 01:48 AM, Pierre wrote:
> Hello,
> I would like to know how to find the difference (set operation)
> between 2 arrays :
> a = array([1,2, 3,2,5,2])
> b = array([1,2])
> I want a - b = [3,5]
> Well, the equivalence of setdiff in matlab...

You will want to ask numpy questions on the numpy mailing list.


Using set() is frequently a good option, but for large arrays, you will want to 
avoid the overhead of converting to and from sets and use numpy.setdiff1d(a, b):

In [2]: a = array([1,2, 3,2,5,2])

In [3]: b = array([1,2])

In [4]: numpy.setdiff1d(a, b)
Out[4]: array([3, 5])

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