0

I have a 2d(3,2) array and a 1d(3,1) array. Between the 2 they share a column of like values. I would like to search the 1d or 2d array for the like value and then return the corresponding element.

arr1=[0,a],[1,b],[2,c]

arr2=[2],[1],[0]

Expected outcome is =[c],[b],[a]
1
  • Do arr1 and arr2 always have the same number of rows? Commented Aug 2, 2019 at 3:15

2 Answers 2

1

You can use numpy array to do this.

import numpy as np

arr1 = [[0, 'a'], [1, 'b'], [2, 'c']]
arr2 = [[2], [1], [0]]

arr1 = np.array(arr1)
arr2 = np.array(arr2)
arr2 = np.squeeze(arr2)

res = arr1[arr2][:,1]

output

array(['c', 'b', 'a'], dtype='<U21')
Sign up to request clarification or add additional context in comments.

Comments

0

The following will return the "value" of an arbitrarily long list, arr1, with "keys" contained in arr1:

for line in arr1:
    if line[0] in arr2:
        print(line[1])

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.