python字符串索引超出范围怎么解决_混淆Python字符串索引超出范围

我看了其他关于这个主题的问题,但找不到真正能解决我所要解决的问题。在

问题是这样的:我正在尝试创建一个程序,在两条互补的DNA链中寻找回文,并返回所识别的每个回文的位置和长度。在

例如,如果给定序列TTGATATCTT,程序应该找到补码(AACTATAGAA),然后将第二个索引标识为6个字符回文的开头。在

我对编程是全新的,所以它可能看起来很傻,但是我想到的代码是这样的:'''This first part imports the sequence (usually consisting of multiple lines of text)

from a file. I have a feeling there's an easier way of doing this, but I just don't

know what that would be.'''

length = 4

li = []

for line in open("C:/Python33/Stuff/Rosalind/rosalind_revp.txt"):

if line[0] != ">":

li.append(line)

seq = (''.join(li))

'''The complement() function takes the starting sequence and creates its complement'''

def complement(seq):

li = []

t = int(len(seq))

for i in range(0, t):

n = (seq[i])

if n == "A":

li.append(n.replace("A", "T"))

if n == "T":

li.append(n.replace("T", "A"))

if n == "C":

li.append(n.replace("C", "G"))

if n == "G":

li.append(n.replace("G", "C"))

answer = (''.join(li))

return(answer)

'''the ip() function goes letter by letter, testing to see if it matches with the letter

x spaces in front of it on the complementary strand(x being specified by length). If the

letter doesn't match, it continues to the next one. After checking all possibilities for

one length, the function runs again with the argument length+1.'''

def ip(length, seq):

n = 0

comp = complement(seq)

while length + n <= (len(seq)):

for i in range(0, length-1):

if seq[n + i] != comp[n + length - 1 - i]:

n += 1

break

if (n + i) > (n + length - 1 - i):

print(n + 1, length)

n += 1

if length <= 12:

ip(length + 1, seq)

ip(length, seq)

当从短序列开始时(例如tcaatgatgcgggtctatagcat),它运行得绝对完美,但是对于较长的序列,我总是得到以下错误消息:

^{pr2}$

在程序检查完可能的4个字符的回文之后,在启动长度为+1的函数之前,将给出消息。在

我明白留言是怎么说的,但我不明白我为什么要收到。为什么这对某些弦有效而对其他弦无效?在过去的一个小时里,我一直在检查序列是奇数个字符还是偶数个字符,是4的倍数,还是4的倍数,等等,我都搞不懂了。我错过了什么?在

任何帮助都将不胜感激。在

另外,问题来自罗莎琳德的网站(罗莎琳德信息),它使用基于1的编号。因此在结尾处打印(n+1,长度)。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值