Home > Software design >  How to get index from value in 2D Numpy array
How to get index from value in 2D Numpy array

Time:01-31

I have two arrays:

values_arr = [[100,1], [20,5], [40,50]...[50,30]]
images_arr = [img1, img2, img3,...imgn]

Both the arrays are numpy arrays.

The values_arr and images_arr are in the same order. i.e

[100, 1] corresponds to img1

How do I get the image given the value of index?

index = [20,5]

In this case, I should get img2 given the value of index = [20,5].

CodePudding user response:

You can use np.where to extract the index of the item :

images_arr[np.where(values_arr == [20,5])[0][0]]

CodePudding user response:

You can make a dict as

values_arr_tup = [tuple(i) for i in values_arr]
dict_ = {key:value for key,value in zip(values_arr_tup ,images_arr)}

then perform dict_[tuple(index)] to get the image

  •  Tags:  
  • Related