Sangam Khanal
Sangam Khanal

Reputation: 133

How to get index from value in 2D Numpy array

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].

Upvotes: 1

Views: 200

Answers (2)

Yadnesh Salvi
Yadnesh Salvi

Reputation: 195

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

Upvotes: 1

sagi
sagi

Reputation: 40491

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

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

Upvotes: 1

Related Questions