[英]Extract indices of multiple elements from 2D Numpy array, Python
I have one 2D numpy array我有一个 2D numpy 数组
import numpy as np
x = np.array([[7, 7, 7],
[4, 7, 7],
[4, 0, 0]])
I extracted y which is我提取了 y 这是
y = [4 4]
Now I want to extract the Indices of [4 4] in x现在我想在 x 中提取 [4 4] 的索引
I am using the following method我正在使用以下方法
indices = np.argwhere(x == y)
which results [[1 0][2 0]
结果
[[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()
您也可以使用
nonzero()
indices = np.nonzero(x == 4)
# (array([1, 2]), array([0, 0]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.