Similar questions have already been asked on SO, but they have more specific constraints and their answers don\'t apply to my question.
Generally speaking, what is t
starting with:
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
What's the most pythonic way to find indices of triplets contained in master? try using np.in1d with a list comprehension:
inds = [j for j in range(len(triangles)) if all(np.in1d(triangles[j], master))]
%timeit says ~0.5 s = half a second
--> MUCH faster way (factor of 1000!) that avoids python's slow looping? Try using np.isin with np.sum to get a boolean mask for np.arange:
inds = np.where(
np.sum(np.isin(triangles, master), axis=-1) == triangles.shape[-1])
%timeit says ~0.0005 s = half a millisecond!
Advice: avoid looping over lists whenever possible, because for the same price as a single iteration of a python loop containing one arithmetic operation, you can call a numpy function that does thousands of that same arithmetic operation
It seems that np.isin(arr1=triangles, arr2=master) is the function you were looking for, which gives a boolean mask of the same shape as arr1 telling whether each element of arr1 is also an element of arr2; from here, requiring that the sum of a mask row is 3 (i.e., the full length of a row in triangles) gives a 1d mask for the desired rows (or indices, using np.arange) of triangles.