I want to delete elements from array A that may be found in array B.
For example:
A = numpy.array([1, 5, 17, 28, 5])
B = numpy.array([3, 5])
C = numpy.delete(A, B)
C= [1, 17, 28]
I want to delete elements from array A that may be found in array B.
For example:
A = numpy.array([1, 5, 17, 28, 5])
B = numpy.array([3, 5])
C = numpy.delete(A, B)
C= [1, 17, 28]
Numpy has a function for that :
numpy.setdiff1d(A, B)
That will give you a new array with the result you expect.
More info on the sciPy documentation
setdiff1d will sort the result and remove duplicates. If you don't want that, A[~numpy.isin(A, B)] will avoid the sort and deduplication, as long as A is actually an array and not the list you put in the question.setdiff1d uses np.unique and np.in1d which turn have Python code. It's heavily dependent on sorting.You can try :
list(set(A)-set(B))
#[1, 28, 17]
Or a list comprehension :
[a for a in A if a not in B]
Another solution :
import numpy
A[~numpy.isin(A, B)]
#array([ 1, 17, 28])
set-based solution does not necessarily preserve the order of the elements in A.Try this
numpy.array([e for e in A if not e in B])