The code I have tried
rand_array2 = np.random.randint(0,3, size=1000)
rand_array2
Searching for a way that allows me to count the (1,1,2)
Sequences in rand_array2
with a for a loop.
The code I have tried
rand_array2 = np.random.randint(0,3, size=1000)
rand_array2
Searching for a way that allows me to count the (1,1,2)
Sequences in rand_array2
with a for a loop.
A possible solution could look like:
import numpy as np
rand_array2 = np.random.randint(0,3, size=1000)
pattern = np.array((1,1,2))
matches=0
for i in range(len(rand_array2)-len(pattern)+1):
match = rand_array2[i:i+3]==pattern
if match.all():
matches+=1
matches
Try this approach. Here we loop through the range of the random array and match any sequences. This should work with any size of sequence as long as it's less than the length of the random array.
seq = (1, 1, 2)
arr = (1, 1, 2, 1, 3, 2, 3, 2, 1, 1, 2, 1)
count = 0
for i in range(len(arr) - len(seq) + 1):
if seq == arr[i:i+len(seq)]:
count += 1
Edit: was a bit late.