简体   繁体   中英

Extract indices of multiple elements from 2D Numpy array, Python

I have one 2D numpy array

import numpy as np
x = np.array([[7, 7, 7],
              [4, 7, 7],
              [4, 0, 0]])

I extracted y which is

y = [4 4]

Now I want to extract the Indices of [4 4] in x

I am using the following method

indices = np.argwhere(x == y)

which results [[1 0][2 0]

But in some cases, this code fails. would you please suggest any other way to perform the same task

You can also use nonzero()

indices = np.nonzero(x == 4)
# (array([1, 2]), array([0, 0]))

Numpy Documentation

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