[英]Array indexing: the fastest way to replace the indexes with another set of indexes in python
[英]Python, The fastest way to find numbers indexes in np. array
在 np 中查找數字索引的最快方法。 Python 中的數組是?
假設我們有一個從 0 到 20 的數字列表,我們想知道數字 2 和 5 的索引
規范的方法是使用 numpy 的where
方法:
a = np.array(range(20))
np.where((a == 2) | (a == 5))
請注意,為了組合(a == 2)
和(a == 5)
這兩項,我們需要按位或運算符|
. 原因是(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.