how to find a sequence of characters in an array and know how many times it appeared

82 Views Asked by At

Please, I need help finding out if the sequence in my array is present in another array, but I need to know how many times this same sequence has been found. I have this but it only returns if found only once.

mylist = ['A','V','V']
# this array contains the sequence 'A','V','V' twice:
listfull = ['A','V','V','V','V','V','E','A','V','V'] 
n = len(mylist ) 
return any(mylist  == listfull [i:i + n] for i in range(len(listfull )-n + 1))

but what I need is to know how many times it appeared in the listfull

1

There are 1 best solutions below

0
On

"... I need to know how many times this same sequence has been found. ..."

Traverse a, qualifying each slice; from i to i + n.

a = ['A','V','V','V','V','V','E','A','V','V']
n = len(b := ['A','V','V'])
x = 0
for i in range(len(a) - n + 1):
    if a[i:i + n] == b: x += 1

Output

2