listA=[1, 2, 2, 3, 1]
listB =[3, 1, 2, 1, 2]
equalfirst = False
equalsecond = False
head_A= 0
end_A = 0
for i in range(1,len(listA)):
for j in range(len(listB),0,-1): ##倒序排列
if listA[0:i] == listB[j:len(listB)]:
head_A = j
equalfirst = True
break
for i in range(1,len(listB)):
for j in range(len(listA),0,-1):
if listB[0:i] == listA[j:len(listA)]:
end_A = i
equalsecond = True
break
print(head_A) # 从前往后数到与A重复,B的索引的起始值
print(“listB后面与A前面重复的部分:”,listB[head_A:len(listB)])
print(end_A ) # 从后往前数到与A重复,B的索引的起始值
print(“listB前面与A后面重复的部分:”,listB[0:end_A])
neg = len(listB)-head_A
if equalfirst == True:
listB = listB[0:-neg]
if equalsecond == True:
listB = listB[end_A:len(listB)]
print(“listB中剩余元素:”,listB)
for i in range(0,len(listB)):
listA.append(listB[i])
print("最终结果为: ",listA)