简体   繁体   中英

Python: Extract the index value from the array by comparing from another array

Python: I have an Array A1 with 3 rows & 2 col and A2 with 1row & 2 columns. Now, I want to print the index of both elements in A2 with respect to A1

Example

A1 = np.array([[0, 6], [15, 1], [1, 15]]) #shape (3,2)
A2 = np.array([15, 1]) #shape(2,)

Expected output (1,0) #Index(position) of array [15,1] in A1

I tried using np.argwhere(A2 == A1).squeeze()

but its not working out as it is writing only A2[0] ie, 15 instead of entire array [15,1]

Please help me with this in python.

Continuation: Python: Extract the index value from the array

import numpy as np

A1 = np.array([[0, 6], [15, 1], [1, 15]]) #shape (3,2)
A2 = np.array([15, 1]) #shape(2,)

a = np.argwhere((A2 == A1).all(1)).squeeze()
print(a)

Output

(1,0)

This code works & checks all the elements in array A2 w.r.t A1 and prints its index thanks to @yann ziselman

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM