Home > Mobile >  Is there a feature that allows me to return the indice of a desired np array in another np array
Is there a feature that allows me to return the indice of a desired np array in another np array

Time:12-28

Assume

Main_Array = np.array([[1,2], [3,4], [4,5], [5,6]])

Further assume

Object_Array = np.array([[1,2]])

I desire a function s.t.

f(Object_Array, Main_Array) = 0

I.e. the indice of the Objective_Array if i know that it is in the Main_Array. I can't seem to use np.where() as it is not vectorized. It does not look for arrays and when i try this in there i get weird results.

Thanks a lot!

CodePudding user response:

Use list.index:

Main_Array = [[1,2], [3,4], [4,5], [5,6]]
Object_Array = [1,2]

Main_Array.index(Object_Array) # Outputs 0

CodePudding user response:

To answer my own question after quite a few attempts the following code works:

index = np.where(np.all(Main_Array == Objective_Array, axis = 1))

then the folowing result is reached

index == [[0]] 

and the desired value can be attained by:

index[0][0] == 0  

If there were no matches it would give:

index == [[]]

So we can use len(index[0]) as a condition for statements since Objective_Array in Main_Array statement usually results in a false positive. If Object_Array were not in main array we would have:

len(index[0]) == 0

If it is in the array we get:

len(index[0]) > 0

In our particular case we had:

len(index[0]) == 1
  • Related