python 识别重叠序列

fd3cxomn  于 2023-02-15  发布在  Python
关注(0)|答案(1)|浏览(134)

我有一个问题,我正在构建一个程序,我想识别两个列表的重叠序列。
序列_1 ='blablablaaaabla'
分隔符_2 ='aaablaccbla'
预期产出为:"啊啊啊"
我创建了这个函数来告诉我sequ_2中的序列是否与sequ_1重叠:
定义重叠(序列_1,序列_2):

count_overlapp = 0 
count_nooverlapp = 0 
for i in range(len(sequ_1)-len(sequ_2)+1): 
    if loci1[i:i+len(sequ_1)]==sequ_2: 
        count_overlapp= count_overlapp+1 
    else: count_nooverlapp = count_nooverlapp +1 
return print(f'The number of overlapped sequence:{count_overlapp}\nThe number of not overlapped sequence:{count_nooverlapp}')`

但是,只有当sequ_2在sequ_1内时,它才计算良好,而如果sequ_2与sequ_1部分重叠,它就不能计算良好
最好的办法是找出重叠的序列
先谢谢你了

osh3o9ms

osh3o9ms1#

也许这个解决方案是你需要的:

sequ_1 = 'blablablaaaabla'
seque_2 = 'aaablaccbla'

overlap = ""

for i in range(len(seque_2)):
    for j in range(i + 1, len(seque_2)):
        tmp = seque_2[i:j+1]
        if len(tmp) > len(overlap) and tmp in sequ_1:
            overlap = tmp

print(overlap)

输出:aaabla

相关问题