Search code examples
pythonarraysnumpynumpy-ndarray

Finding specific array or list in a numpy array


I have a list of numpy arrays, which consists of all possible configurations of 0 and 1 in a 10-pixel arrays. I'm trying to determine the number of arrays that have specific group of 1s for more than two 1s. For example, the array is [1,0,0,1,1,1,1,1,0,1]. I want to determine this array has five 1s as a block. Another example, the array is [1,1,1,0,1,1,1,1,1,1]. I want to find the block as six 1s instead of three 1s block. I couldn't find a way to do this.

Here is the code I generate the list of all possible arrays:

import numpy as np
from itertools import product

all_arrays = np.array(list(product([0,1], repeat=10)))

Solution

  • convert the array to a string and check for membership along the lines of:

    str_array = ''.join(my_array)
    return 5*'1' in str_array