[英]How to check efficiently if numpy array a contains b
How to check if one numpy array a contains fully another numpy array b efficiently? 如何检查一个numpy数组a是否有效地包含另一个numpy数组? Somewhat like b is subset of a....
有点像b是a的子集....
Thanks! 谢谢!
EDIT: a and b are one dimentional numpy arrays 编辑:a和b是一维numpy数组
If you're asking about b
being a consecutive sub-array of a
如果你问
b
是一个连续的子阵列 a
If either of the arrays can contain repeated values, algorithmically speaking, your problem is equivalent to a single-pattern string-searching problem . 从算法上讲,如果其中一个数组可以包含重复值,则问题等同于单模式字符串搜索问题 。 There are several known algorithms for this problem.
这个问题有几种已知的算法。 Unfortunately, neither is too simple.
不幸的是,两者都不太简单。
Else, it is simple to implement, by first looking for the first element in b
, then comparing all the following elements: 另外,通过首先查找
b
的第一个元素,然后比较以下所有元素,实现起来很简单:
import numpy as np
def is_subarray_no_repeatition(a, b):
try:
i = np.where(a == b[0])[0][0]
except IndexError:
# either b is empty, or b[0] not in a
return b.size == 0
a = a[i : i+b.size]
if a.size < b.size:
return False
return (a == b).all()
If you're asking about b
being a subset of a
(ie that each element of b
exists in a
) 如果你问
b
是的一个子集 a
(即每个元素b
中存在a
)
def is_subset(a, b):
b = np.unique1d(b)
c = np.intersect1d(a,b)
return c.size == b.size
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.