如何在数组中查找字符序列并知道它出现了多少次

问题描述 投票:0回答:1

拜托,我需要帮助找出我的数组中的序列是否存在于另一个数组中,但我需要知道相同的序列被发现了多少次。我有这个,但只有找到一次才会返回。

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

但我需要的是知道它在列表中出现了多少次

python arrays sorting any findelement
1个回答
0
投票

尝试此代码

mylist = ['A', 'V', 'V']
listfull = ['A', 'V', 'V', 'V', 'V', 'V', 'E', 'A', 'V', 'V']

n = len(mylist)
count = 0

for i in range(len(listfull) - n + 1):
    if mylist == listfull[i:i + n]:
        count += 1

print(count)
© www.soinside.com 2019 - 2024. All rights reserved.