【发布时间】:2020-04-13 14:33:54
【问题描述】:
这里是 Python 新手。我有一个问题,我想在一个列表中找到所有重复的模式(特别是在我的情况下,它是一个整数列表)。因此,例如,给定列表 [2,1,4,3,12,8,3,3,4,16,2,9,9,8,3,3,4,1,4,3,4 ,8,3,3,4] 并且最小模式长度为 3,算法会发现 [8,3,3,4] 出现三次,[1,4,3] 出现两次(也很高兴有索引所有出现)。
我有一些可以工作的代码,虽然有点笨拙,但我希望最终使用这些代码的列表可能非常大。我不太确定如何计算代码的操作复杂性,但我知道当我使用大型列表时它肯定会变得非常慢。
我的问题是,有没有人知道的更好的算法可以做到这一点,和/或我是否以非常低效的方式做到这一点?感谢您能给我的任何帮助。
代码如下:
# Searches list to determine how many times small list is included in big list
def contains(small, big):
counter = 0
# initiating list of indexes. N.B. indexlist gives LAST index of sequence, not first
indexlist = []
for i in range(len(big)-len(small)+1):
for j in range(len(small)):
if big[i+j] != small[j]:
break
else:
counter += 1
indexlist.append(i+j)
if counter > 0:
return counter, indexlist
return False
def findrepeats(sequence, n_letters):
fulldict = {}
# Iterating through all the short-sequences of n letters in the list
for i in range(0, len(sequence) - n_letters):
shortliststr = ""
shortlist = sequence[i:i + n_letters]
for number in shortlist:
shortliststr = shortliststr + "." + str(number)
# If short-sequence is found in full sequence more than once (i.e. itself), add to dict
if contains(shortlist, sequence)[0] > 1 and len(shortlist) == n_letters:
fulldict[shortliststr] = contains(shortlist, sequence)
return fulldict
def findallrepeats(sequence, min_letters, max_letters):
fulldict = {}
# Iterating through all possible n_letters in findrepeats() between given range
for i in range(min_letters, max_letters):
newdict = findrepeats(sequence, i)
fulldict.update(newdict)
return fulldict
【问题讨论】:
-
您可以使用后缀自动机en.wikipedia.org/wiki/Suffix_automaton,就像您的数组是一个字符串并且其中的数字是字母一样。它会给出(有点)O(n) 预先计算的复杂性和 O(模式的长度) 查找出现次数的复杂性。