[英]Python, The fastest way to find numbers indexes in np. array
The fastest way to find numbers indexes in np.在 np 中查找数字索引的最快方法。 array in Python is?
Python 中的数组是?
Suppose we have a list of numbers from 0 to 20, and we want to know the indexes of digits 2 and 5假设我们有一个从 0 到 20 的数字列表,我们想知道数字 2 和 5 的索引
The canonical way would be to use numpy's where
method:规范的方法是使用 numpy 的
where
方法:
a = np.array(range(20))
np.where((a == 2) | (a == 5))
Note that in order to combine the two terms (a == 2)
and (a == 5)
we need the bitwise or operator |
请注意,为了组合
(a == 2)
和(a == 5)
这两项,我们需要按位或运算符|
. . The reason is that both
(a == 2)
and (a == 5)
return a numpy array of dtype('bool')
:原因是
(a == 2)
和(a == 5)
都返回 dtype dtype('bool')
的 numpy 数组:
>>> a == 2
array([False, False, True, False, False, False, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False])
>>> (a == 5)
array([False, False, False, False, False, True, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False])
>>> (a == 2) | (a==5)
array([False, False, True, False, False, True, False, False, False,
False, False, False, False, False, False, False, False, False,
False, False])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.