Academia
Academia

Reputation: 4124

excluding element from numpy array

I want to get the c array as result, but I don't know how:

import numpy as np
a = xrange(10)
b = np.array([3,2,1,9])

c is made of elements of a that are not in b:

c = np.array([0,4,5,6,7,8])

Upvotes: 3

Views: 5639

Answers (2)

JoshAdel
JoshAdel

Reputation: 68742

Perhaps a more straightforward solution is the following:

import numpy as np
a = xrange(10)
b = np.array([3,2,1,9])

c = np.setdiff1d(a,b)

Which results in:

In [7]: c
Out[7]: array([0, 4, 5, 6, 7, 8])

You can find all of the set-like operations for numpy arrays in the documentation: http://docs.scipy.org/doc/numpy/reference/routines.set.html

Upvotes: 10

eumiro
eumiro

Reputation: 213115

import numpy as np
a = np.arange(10)
b = np.array([3,2,1,9])

np.array(sorted(set(a) - set(b)))
# array([0, 4, 5, 6, 7, 8])

UPDATE: works with a = xrange(10) too.

Upvotes: 3

Related Questions