我有一个整数列表,我需要删除重复的序列,逻辑正在我的脑袋里。
我一直在尝试将this修改为我需要的内容,但是只返回一个与重复序列长度相关的数字,并且它只从起始整数开始计算。
到目前为止,这是我所知道的:
def findRep(rmRepList):
#Array to hold [starting position, length] of repeating sequences
repList = []
#For each industry listed
for industry in rmRepList:
#Maximum starting position
maxStartPos = len(industry)-2
#For each possible starting point of repetition
for start in range(1,maxStartPos):
#Limit on how long the repetition can be
maxLen = math.ceil((len(industry)-start)/2)
#For each possible length (2 because already canceled out repeating resources in genAllLoop)
for i in range(2,maxLen):
#If the next 'i' integers repeat
if industry[start:i+start] is industry[i+start:2*i+start]:
repList = [start,i]
industry = rmRep(repList, industry)
#If reached end of list
if 2*i+start+1 == len(industry):
#End loop
break
def rmRep(rmProp, loop):
#Sequence of resources to drop
rmSeq = [loop[rmProp[0]:rmProp[0]+rmProp[1]]]
#Debugging statement
print(rmSeq)
loop.remove(rmSeq)
return(loop)name = username_entry.get()
rmRepList是一个列表列表,包含我需要分析的每个列表。
例如,如果给出一个列表
rmRepList = [[0,1,2,1,2,1,0],[0,1,2,1,2,1,2,3,4,5,3,4,5,6,0]]
我需要它返回[[0,1,2,1,0], [0,1,2,3,4,5,6,0]]
现在,代码甚至没有到达rmRep子例程。我有一种可怕的感觉,我说这一切都错了。我不喜欢我在代码中需要这么多循环,特别是因为我必须分析的实际列表长达数百个数字。有更简单的方法吗?
编辑:如果有帮助,我可以保证列表不会反复重复一个整数(例如[0,0,0,1,0]
不会发生)。
此外,列表中的第一个和最后一个数字将始终相同。
答案 0 :(得分:2)
部分答案:检测重复的序列。
listA = [0,1,2,1,2,1,2,3,4,5,3,4,5,6,0]
listB = [0,1,2,1,2,1,0]
def get_repeated_seq(seq, start, length):
ref = seq[start:start+length]
#print("Ref", ref)
for pos in range(start+length, len(seq)-length):
compare = seq[pos:pos+length]
#print("Pos", pos, compare)
if compare == ref:
print("Found", ref, "at", pos)
return pos
return False
def get_repeated_seqs(seq):
for size in reversed(range(2, len(seq)/2)):
for pos in range(0, len(seq)-size):
print("Check rep starting at pos %s for size %s" % (pos, size))
get_repeated_seq(seq, pos, size)
print(get_repeated_seqs(listA))
然后你可以根据你的删除策略删除它们(bigsts?smallests?)
编辑:要清楚它是否有效(并在同一时间添加一些删除)
listA = [0,1,2,1,2,1,2,3,4,5,2,1,3,4,5,2,1,6,0]
listB = [0,1,2,1,2,1,0]
def get_repeated_seq(seq, start, length):
ref = seq[start:start+length]
#print("Ref", ref)
for pos in range(start+length, len(seq)-length):
compare = seq[pos:pos+length]
#print("Pos", pos, compare)
if compare == ref:
#print("Found", ref, "at", pos)
return pos, length
return False
def get_repeated_seqs(seq):
reps = []
for size in reversed(range(2, len(seq)/2)):
for pos in range(0, len(seq)-size):
#print("Check rep starting at pos %s for size %s" % (pos, size))
rep = get_repeated_seq(seq, pos, size)
if rep:
reps.append(rep)
return reps
def remove_repeated_seqs(seq, reps):
# need to backup seq ?
for rep in reps:
overlaps = False
for pos in range(rep[0], rep[0]+rep[1]):
if seq[pos] == "*":
overlaps = True
if not overlaps:
for pos in range(rep[0], rep[0]+rep[1]):
seq[pos] = "*"
out = []
for item in seq:
if item != "*":
out.append(item)
return out
reps = get_repeated_seqs(listB)
rem = remove_repeated_seqs(listB, reps)
#print(rem)
print(rem==[0,1,2,1,0])
reps = get_repeated_seqs(listA)
rem = remove_repeated_seqs(listA, reps)
#print(rem)
print(rem==[0,1,2,3,4,5,6,0])
输出True和True:)
EDIT2:没有-1到达范围循环中列表的末尾..