1

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

2
  • You can use boolean indexing: images_arr[(values_arr == [20,5]).all(axis=1)] Commented Jan 30, 2022 at 9:59
  • Encode your keys into a single integer for faster lookup. What have you tried? Commented Jan 30, 2022 at 10:17

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

TypeError: unhashable type: 'numpy.ndarray'
That is because of list cannot be key in python, so I made some changes to my answer.
1

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.