我试图找到两个DNA序列的LCS。我输出矩阵形式以及包含最长公共序列的字符串。但是,当我在代码中返回矩阵和列表时,我得到以下错误:IndexError:字符串索引超出范围最长的通用序列 - 语法错误
如果我要删除涉及变量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
但我开始了与什么在字符串中,因为我希望程序在进行for循环时填充最长的序列(当然,用已经保存的序列之前的序列替换最长的序列) – Roy 2014-09-29 01:25:17
Python将首先执行最内层的操作,温度[I-1] [j-1]。在那个操作的时候,temp =='',它没有可以通过将索引号传递给列表来找到的字符串(调用'any_string [any_integer]'将any_string作为字符列表存储,即['H ”, 'E', 'L', 'L', 'O'])。我没有遗传学背景,所以我不确定你想要加入到现有的临时字符串中,但是通过索引查找空字符串中的字符将永远不会工作。你能提供一个你想看到的输出的例子吗? – 2014-09-29 01:33:20
看到这个页面http://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Longest_common_substring#Python – han058 2014-09-29 01:35:16