I have two 2d numpy arrays, a and b. I want to compare if every element in b array is in array a. For example, if [2,1] is in [1,4,3,2] the exit will be True, if [2,1] is in [3,1] the exit will be False etc... Same procedure with [1,3] element. The exit has to be [True, False, False] for [2,1] element and [True, True, False] for [1,3] element.
a = np.array([[1,4,3,2],[3,1],[1,7,8,9,4]])
b = np.array([[2,1],[1,3]])
[True,False,False]
[True,True,False]
A simple way would be -
In [80]: np.vstack([np.isin(b,i).all(1) for i in a])
Out[80]:
array([[ True, True],
[False, True],
[False, False]])
Take a look at the numpy.in1d function.. it checks if elements from the first array are contained in the second array:
In [18]: [[np.in1d(bx, ax).all() for ax in a] for bx in b]
Out[18]: [[True, False, False], [True, True, False]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.