[英]Python quick search in numpy array
Does Numpy have a function for quick search of element in 2D array and return its indexes? Numpy是否具有在2D数组中快速搜索元素并返回其索引的功能? Mean for example: 均值例如:
a=54
array([[ 0, 1, 2, 3],
[ 4, 5, 54, 7],
[ 8, 9, 10, 11]])
So equal value will be array[1][2]
. 所以相等的值将是array[1][2]
。 Of course I can make it using simple loops- but I want something similar to: 当然,我可以使用简单的循环来实现它-但我想要类似的东西:
if 54 in arr
In [4]: import numpy as np
In [5]: my_array = np.array([[ 0, 1, 2, 3],
[ 4, 5, 54, 7],
[8, 54, 10, 54]])
In [6]: my_array
Out[6]:
array([[ 0, 1, 2, 3],
[ 4, 5, 54, 7],
[ 8, 54, 10, 54]])
In [7]: np.where(my_array == 54) #indices of all elements equal to 54
Out[7]: (array([1, 2, 2]), array([2, 1, 3])) #(row_indices, col_indices)
In [10]: temp = np.where(my_array == 54)
In [11]: zip(temp[0], temp[1]) # maybe this format is what you want
Out[11]: [(1, 2), (2, 1), (2, 3)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.