简体   繁体   中英

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? Somewhat like b is subset of a....

Thanks!

EDIT: a and b are one dimentional numpy arrays

If you're asking about b being a consecutive sub-array of 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:

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 )

def is_subset(a, b):
  b = np.unique1d(b)
  c = np.intersect1d(a,b)
  return c.size == b.size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM