Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, how do I find the elements in array B that are not in array A?

I'm trying to find the difference between the 2 arrays

arrayA = np.array(['A1', 'A2', 'A3'])
arrayB = np.array(['A1', 'A2', 'A3', 'A4', 'A5', 'A6'])

I'm trying to get

difference = ['A4', 'A5', 'A6']

How can I do this? Thank you.

like image 689
Lê Trần Phú Avatar asked Dec 19 '25 02:12

Lê Trần Phú


2 Answers

Use numpy's setdiff:

np.setdiff1d(arrayA, arrayB)

Also - is there any special reason for which this needs to be a numpy array? You could simply use sets and then the minus operator: set(arrayA) - set(arrayB)

like image 196
Gilad Green Avatar answered Dec 20 '25 15:12

Gilad Green


[i for i in arrayB if i not in arrayA]
like image 23
WSUN000 Avatar answered Dec 20 '25 16:12

WSUN000



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!