【发布时间】:2022-01-25 15:20:05
【问题描述】:
我正在尝试创建两个列表,其中包含字符串的“开始”和“结束”索引。 在这种情况下,两个字符串的长度相等。 例如
str1='ATGGATCGATCG'
str2='CGGGCGCGCGCG'
这里,匹配的长度是:GG、CG、CG
我想要以下类型的输出:
list = [2,3,6,7,10,11] #list of the matched indices
start = [2,6,10] #start indices of the matched lengths
end = [3,7,11] #end indices if the matched lengths
现在,我的代码块如下所示,但我希望索引能够定位匹配的序列。
str1='ATGGATCGATCG'
str2='CGGGCGCGCGCG'
result1 = ''
result2 = ''
#handle the case where one string is longer than the other
maxlen=len(str2) if len(str1)<len(str2) else len(str1)
#loop through the characters
for i in range(maxlen):
letter1=str1[i:i+1]
letter2=str2[i:i+1]
if ((letter1 == letter2) and letter1 in ['A','T','C','G'] and letter2 in ['A','T','C','G']):
result1+=letter1
result2+=letter2
【问题讨论】:
-
你想要任意长度的字符串匹配吗?还是您在寻找长度为 2 的匹配项?
-
你的比赛总是成对的吗?例如,如果您的字符串是
'AAAB'和AAAC。您想要的索引列表[1, 2, 3]或[1, 3]是什么? -
@MoinuddinQuadri [1, 3]