Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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])
like image 460
Academia Avatar asked Aug 01 '26 15:08

Academia


1 Answers

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

like image 115
JoshAdel Avatar answered Aug 04 '26 06:08

JoshAdel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!