簡體   English   中英

使用numpy測試一個數組中的每個元素是否存在於另一個數組中的最有效方法

[英]Most efficient way to test whether each element from one array exists in a another array, using numpy

我想知道確定一維numpy數組中的元素是否存在於另一個數組中的最有效方法。

具體來說,我有兩個numpy 1-d數組。 第一個是未排序的整數數組。 第二個是目標值的排序數組。

輸入樣例:

[45982, 124, 12, 1092, 45982, 1, 985, 299, 10092] # array 1

[1, 12, 299] # array 2

預期輸出(即數組2中的數組1元素):

[False, False, True, False, False, True, False, True, False]

實際的數組將更長:數組1可能包含> 5,000,000個元素,數組2可能包含500,000至1,000,000個元素。

也許np.in1d

>>> xs = np.array([45982, 124, 12, 1092, 45982, 1, 985, 299, 10092])
>>> ys = np.array([1, 12, 299])
>>> np.in1d(xs, ys)
array([False, False,  True, False, False,  True, False,  True, False], dtype=bool)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM