Home > database >  Extract the index value from array
Extract the index value from array

Time:12-23

I have an array A1 with shape 2x3 & list A2. I want to extract the index value of array from the list.

Example

A1 = [[0, 1, 2]
     [3, 4, 5]] # Shape 2 rows & 3 columns

A2 = [0,1,2,3,4,5]

Now, I want to write a code to access the an element's index in Array A1

Expected Output

A2[3] = (1,0) #(1 = row & 0 = column) Index of No.3 in A1

Please help me. Thank you

CodePudding user response:

You can use unravel_index for that.

Example:

>>> np.unravel_index(3, A1.shape)
(1, 0)

CodePudding user response:

If you can use numpy, check out argwhere

a1 = np.array([[0,1,2],[3,4,5]])
a2 = [0,1,2,3,4,5]
a3 = np.argwhere(a1 == a2[3]).squeeze() # -> (1, 0)
  • Related