Python:识别和删除列表中的重复序列

Python:识别和删除列表中的重复序列,python,list,sequences,subsequence,Python,List,Sequences,Subsequence,我正在寻找在列表中找到重复序列的最佳方法。 序列定义为至少两个相邻值 示例:在下面的列表中,应标识并删除重复序列 a = [45874, 35195, # <- 28965, 05867, 25847, # <- 94937, 64894, 55535, 62899, 00391, 35195, # Duplicate Sequence Start 28965, 05867, 25

我正在寻找在列表中找到重复序列的最佳方法。 序列定义为至少两个相邻值

示例:在下面的列表中,应标识并删除重复序列

a = [45874,
    35195, # <-
    28965,
    05867,
    25847, # <-
    94937,
    64894,
    55535,
    62899,
    00391,
    35195, # Duplicate Sequence Start
    28965,
    05867,
    25847, # Duplicate Sequence End
    08483,
    55801,
    33129,
    42616]
a=[45874,

35195,#我使用了以下方法,可能效率不高:

sequences=[] #all the sequences, as defined in question
duplicated=[] #all the duplicated sequences
for i in range(2, len(a)): #possible lengths of sequence
    n=0 # index of start of sequence
    for j in a:
        if n+i<=len(a): #if it's not too close to the end
            sequences.append(a[n:n+i]) #add the sequence
        n+=1
tests=sequences[:] #duplicate so that I can remove for checking purposes
for i in sequences:
    tests.remove(i)
    if i in tests: #if it's in there twice
        duplicated.append(i) #add it to the duplicates
for i in duplicated:
    found=False #haven't found it yet
    n=0 #index we're at
    for j in a:
        if a[n:n+len(i)]==i and not found: #if it matches
            del a[n:n+len(i)] #remove it
            found=True #don't remove it again
        n+=1
sequences=[]#所有序列,如所述
duplicated=[]#所有重复序列
对于范围(2,len(a))中的i:#序列的可能长度
n=0#序列开始索引
对于a中的j:

如果n+i在长度为n的字符串中查找长度为m的单个子序列可以在不小于O(nm)的时间内完成。查找所有重复的子序列很可能不止于此。尽管,如果您只想删除子序列而不查找它们,有一个技巧

删除O(n)中的重复子序列 我们只需要关注长度为2的子序列,因为任何序列都可以表示为长度为2的重叠子序列。这一观察结果允许我们对这些对进行计数,然后从右向左移除它们

特别是,这只需要遍历列表固定的次数,因此是O(n)

下面是一个基于提供的输入的示例

a = [45874,
     35195, # This
     28965, # Is
     5867,  # A
     25847, # Subsequence
     94937,
     64894,
     55535,
     62899,
     391,
     35195, # Those
     28965, # Should
     5867,  # Be
     25847, # Removed
     8483]

b = remove_duplicates(a)
# Output:
#   [45874,
#    35195,
#    28965,
#    5867,
#    25847,
#    94937,
#    64894,
#    55535,
#    62899,
#    391,
#            <- Here the duplicate was removed
#    8483]
a=[45874,
35195#这个
28965#是
5867#A
25847#子序列
94937,
64894,
55535,
62899,
391,
35195#那些
28965#应该
5867#Be
25847,已删除
8483]
b=删除重复项(a)
#输出:
#   [45874,
#    35195,
#    28965,
#    5867,
#    25847,
#    94937,
#    64894,
#    55535,
#    62899,
#    391,

#你尝试过什么?重叠的重复序列又如何?整个过程是被删除了还是只是一个例子[1,2,2,3,1,2,3]。它应该产生[1,2,2,3]还是[1,2,2,3,3]还有这些字符串吗?因为你不能输入以0开头的整数。我觉得对分模块有一些事情要做…你想过吗?我正试图想出一些办法,但确实很棘手…它会删除第一个而不是第二个外观,不知道这是否是个问题。。。
a = [45874,
     35195, # This
     28965, # Is
     5867,  # A
     25847, # Subsequence
     94937,
     64894,
     55535,
     62899,
     391,
     35195, # Those
     28965, # Should
     5867,  # Be
     25847, # Removed
     8483]

b = remove_duplicates(a)
# Output:
#   [45874,
#    35195,
#    28965,
#    5867,
#    25847,
#    94937,
#    64894,
#    55535,
#    62899,
#    391,
#            <- Here the duplicate was removed
#    8483]