python - check if numpy array is subset of another array -


similar questions have been asked on so, have more specific constraints , answers don't apply question.

generally speaking, pythonic way determine if arbitrary numpy array subset of array? more specifically, have 20000x3 array , need know indices of 1x3 elements entirely contained within set. more generally, there more pythonic way of writing following:

master=[12,155,179,234,670,981,1054,1209,1526,1667,1853] #some indices of interest triangles=np.random.randint(2000,size=(20000,3)) #some data i,x in enumerate(triangles):   if x[0] in master , x[1] in master , x[2] in master:     print 

for use case, can safely assume len(master) << 20000. (consequently, safe assume master sorted because cheap).

you can via iterating on array in list comprehension. toy example follows:

import numpy np x = np.arange(30).reshape(10,3) searchkey = [4,5,8] x[[0,3,7],:] = searchkey x 

gives

 array([[ 4,  5,  8],         [ 3,  4,  5],         [ 6,  7,  8],         [ 4,  5,  8],         [12, 13, 14],         [15, 16, 17],         [18, 19, 20],         [ 4,  5,  8],         [24, 25, 26],         [27, 28, 29]]) 

now iterate on elements:

ismember = [row==searchkey row in x.tolist()] 

the result is

[true, false, false, true, false, false, false, true, false, false] 

you can modify being subset in question:

searchkey = [2,4,10,5,8,9]  # add more elements testing setsearchkey = set(searchkey) ismember = [setsearchkey.issuperset(row) row in x.tolist()] 

if need indices, use

np.where(ismember)[0] 

it gives

array([0, 3, 7]) 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -