我试图找到两个 DNA 序列的 LCS。我正在输出矩阵形式以及包含最长公共序列的字符串。但是,当我在代码中同时返回矩阵和列表时,我收到以下错误:IndexError: string index out of range
如果我要删除涉及变量 temp 和 higestcount 的编码,我的代码将很好地输出我的矩阵。我正在尝试对矩阵使用类似的编码来生成我的列表。有没有办法避免这个错误?根据序列 AGCTGGTCAG 和 TACGCTGGTGGCAT,最长的公共序列应该是 GCTGGT。
def lcs(x,y):
c = len(x)
d = len(y)
plot = []
temp = ''
highestcount = ''
for i in range(c):
plot.append([])
temp.join('')
for j in range(d):
if x[i] == y[j]:
plot[i].append(plot[i-1][j-1] + 1)
temp.join(temp[i-1][j-1])
else:
plot[i].append(0)
temp = ''
if temp > highestcount:
highestcount = temp
return plot, temp
x = "AGCTGGTCAG"
y = "TACGCTGGTGGCAT"
test = compute_lcs(x,y)
print test