Home > Net >  Finding specific array or list in a numpy array
Finding specific array or list in a numpy array

Time:11-01

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)))

CodePudding user response:

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

CodePudding user response:

Try below approach. Its not very efficient, but it should work.

from functools import reduce
import numpy as np
from itertools import product

n=int(input("How many 1s you want?"))
all_arrays = list(filter(lambda x : n*"1" in x and (n 1)*"1" not in x,map(lambda e : reduce(lambda x,y : str(x) str(y), e), product([0,1], repeat=10))))
print(len(all_arrays))
  • Related