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 (i.e. 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
```

Similar Questions

Given an array {1,4,5,6,7,4,7,8} How to check if there are values between 4 and 7 ?

I have an array and its length is X. Each element of the array has range 1 .. L. I want to iterate efficiently through all array combinations that has sum L. Correct solutions for: L = 4 and X = 2 1 3

I have a two numpy.arrays, I want to get following result efficiently 1.add the element's of b to a's sub-array a=numpy.array([(1,2,3),(1,2,3)]) b=numpy.array([0,0]) -> c=[(0,1,2,3),(0,1,2,3)] co

How to xor all elements of a boolean numpy array using vectorized methods: i.e., a_1 xor a_2 xor ... xor a_n?

At first sight it's very simple, but I'm having some problems to do this without using a lot of nested loops. Example: var father:Array = new Array(0,1,2,3,4,5); var son:Array = new Array(3,4,5); fath

We initialize a numpy array with zeros as bellow: np.zeros((N,N+1)) But how do we check whether all elements in a given n*n numpy array matrix is zero. The method just need to return a True if all th

I want to check if my string contains a + character.I tried following code s= ddjdjdj+kfkfkf; if(s.contains (\\+){ String parts[] = s.split(\\+); s= parts[0]; // i want to strip part after + } b

I need to generate a lot of random numbers. I've tried using random.random but this function is quite slow. Therefore I switched to numpy.random.random which is way faster! So far so good. The generat

I want to check if a numpy array is multidimensional or not? V = [[ -7.94627203e+01 -1.81562235e+02 -3.05418070e+02 -2.38451033e+02][ 9.43740653e+01 1.69312771e+02 1.68545575e+01 -1.44450299e+02][ 5.6

This problem seems easy but I cannot quite get a nice-looking solution. I have two numpy arrays (A and B), and I want to get the indices of A where the elements of A are in B and also get the indices

So here is my example string test = Hello World, I am testing this string.; string[] myWords = {testing, string}; How do I check if the string test contains any of the following words? If it do

How do I create a 0 x 0 (i.e. ndim = 2, shape = (0,0)) numpy.ndarray of float?

Given that I have a numpy array of three dimension [3,500,500], how can I extract one dimension as [1,500,500]? import numpy as np my_array = np.ones((3,500,500),dtype=int,order='C') print (my_array)

How order of numpy array influence on multiplication speed? And how I can auto choose it depending on size of matrices? The question initially comes from code using cudamat: def test_mat(): #need to i

Given the following two csv files that contain strings only, how can I load them into an (numpy) array? **1.txt** A,B,D E,G,A **2.txt** A,B,D E,G,A **data** 1,A,B,D 1,E,G,A 2,A,B,D 2,E,G,A

I have a question about Java maps. I use map to contain array, and I want to check whether the map contains the array I want. But it does not work. Is there anyway to check whether the map contains th

I am using mongodb now. I have blogpost collection, and blogpost has a tags filed which is an array, e.g. blogpost1.tags = ['tag1', 'tag2', 'tag3', 'tag4', 'tag5'] blogpost2.tags = ['tag2', 'tag3'] bl

Given an array d = np.random.randn(100) and an index array i = np.random.random_integers(low=3, high=d.size - 5, size=20) how can I efficiently create a 2d array r with r.shape = (20, 8) such that

I have a numpy 2d array A, and a list of row numbers row_set. How can I get new array B such as if row_set = [0, 2, 5], then B = [A_row[0], A_row[2], A_row[5]]? I thought of something like this: def s

I'm wondering how to get a random numpy array of integers using DNA bases. I have the basic numpy function working, but I can't accomplish this without transforming the numpy array into a list of stri

How to write multiple numpy arrays into one csv file in multiple columns？ import numpy import csv arrA = numpy.array(file.root.a) arrB = numpy.array(file.root.b) arrC = numpy.array(file.root.c) for i

So I'm querying database, and it will return an array of id's. $val = $db->prepare(SELECT x FROM xx WHERE x_id='$xx'); $val->execute(); $res = $val->fetchAll(PDO::FETCH_COLUMN, 0); Then,

Is there a quick way to sub-flatten or flatten only some of the first dimensions in a numpy array? For example, given a numpy array of dimensions (50,100,25), the resultant dimensions would be (5000

In Python numpy.unique can remove all duplicates from a 1D array, very efficiently. 1) How about to remove duplicate rows or columns in a 2D array? 2) How about for nD arrays?

I'm trying to figure out an efficient way of taking a numpy array of float values and converting them to an index associated with a specific range. eg numpy array of x floats [ -999.99, 433.000, -56.0

Let's say I have the following 2D numpy array consisting of four rows and three columns: >>> a = numpy.arange(12).reshape(4,3) >>> print(a) [[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] W

I have a 4-D(d0,d1,d2,d3,d4) numpy array. I want to get a 2-D(d0,d1)mean array, Now my solution is as following: area=d3*d4 mean = numpy.sum(numpy.sum(data, axis=3), axis=2) / area But How can I use

How do I check if string contains \n or new line character ? word.contains(\\n) word.contains(\n)

I'm a newbie in c# and i want to figure out how to check if a char array values are entirely composed of number/numeric/digits I tried this code : bool t=true; for (int k = 0; k < chain.Length; k+

For example: I have a = array([123, 412, 444]) and b = array([123, 321]) I want to know if a contains all the elements in b. Is there a simple operation for this? In this case that would not be true T

How to check if class type B extends class type A in groovy? class A { } class B extends A { }

How do I check if a string contains of at least one Chinese character?

Suppose I have a list contains un-equal length lists. a = [ [ 1, 2, 3], [2], [2, 4] ] What is the best way to obtain a zero padding numpy array with standard shape? zero_a = [ [1, 2, 3], [2, 0, 0],

$foo = array('one'); $foofoo = array('onekey' => 'onevalue'); How do you check if an array contains only one key?

What's the best way to convert numpy's recarray to a normal array? i could do a .tolist() first and then do an array() again, but that seems somewhat inefficient.. Example: import numpy as np a = np.r

ls_ord_symbols = np.zeros((len(na_csv_orders), 1), dtype='S5') >>> print ls_ord_symbols [['AAPL'] ['IBM'] ['GOOG']] >>> print type(ls_ord_symbols) <type 'numpy.ndarray'> >&g

I have a very (very, very) large two dimensional array - on the order of a thousand columns, but a couple of million rows (enough so that it doesn't fit in to memory on my 32GB machine). I want to com

In Bash, what is the simplest way to test if an array contains a certain value? EDIT: with help from the answers and the comments, after some testing, I came up with this: function contains() { local

I have a Python 3 dictionary holding very long lists (30 million integers each). I would like to stitch all these lists into a single numpy array. How can I do this efficiently? The following np.array

How can I check if a XML node contains text, or only empty nodes? Example: Let's say we have the following XML: <text> <p> </p> <p> </p> </text> (Note the whitespa

How should I test if an array contains at least 1 element (rather than just being an empty array $myarray = array();)? Is there a THE way? E.g. if ($myarray) { } if (count($myarray)) { } if (count($my

How can I define _SOME CODE_ in the next code fragment in order to get the results shown below? vector = numpy.array([a,b,c,d]) for i in xrange(4): print vector[_SOME CODE_ using i] It sould give me

How would i format a numpy array of form data1 = np.array([[0,0,0],[0,1,1],[1,0,1],[1,1,0]]) to a list in this format: data = [ [[0,0], [0]], [[0,1], [1]], [[1,0], [1]], [[1,1], [0]] ] I tried usin

I two numpy arrays, both M by N. X contains random values. Y contains true/false. Array A contains indices for rows in X that need replacement, with the value -1. I want to only replace values where Y

I am using below code to check ary_navigationControllerViews array contains myclass object or not its working fine but i need with out for loop.I know we have method like containsObject but how to use

I have a text input that search subjectid,subjectname , semester/year and want to search everything in a single input. 1/2010 -/---- I need to check the result is true or false . How to do that?

I am still writing on a python interface for my c code with ctypes. Today I substituted my file reading function with a python version, which was programmed by somebody else usind NumPy. The 'old' c v

I would like to create an 3D array in numpy as follow : [ 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 ] ... Is there a nice way to write it ?

I currently have two large numpy arrays of equivalent lengths. The first array is filled with values in sets of 5 that will either be a set of 5 float values or 5 0s as such: [ [.03, 5, .1, 0.23, 5],

connum = np.empty((160,160)) confidence = [2142,213124,23123,34324,4545,56634,343,45435,12312,4543,343] I have the list confidence and the numpy array connum how can i populate the empy numpy array w