Numpy是否具有快速搜索2D数组中的元素并返回其索引的功能? 意思是例如:
var arrs = [];
jQuery('.option').each(function(){
arrs.push(jQuery(this).val());
});
var arrayLength = arrs.length;
var retval = "";
for (var i = 0; i < arrayLength; i++) {
retval = arrs[i] + '|';
//Do something
}
alert(retval);
所以同等价值将是a=54
array([[ 0, 1, 2, 3],
[ 4, 5, 54, 7],
[ 8, 9, 10, 11]])
。
当然,我可以使用简单的循环 - 但我想要类似的东西:
array[1][2]
答案 0 :(得分:2)
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)]