简体   繁体   English

IndexError:字符串索引超出范围?

[英]IndexError: string index out of range?

I keep getting an IndexError: string index out of range exception on the following code and I cannot figure out why. 我继续在下面的代码中得到一个IndexError: string index out of range异常,我无法弄清楚原因。

I am supposed to solve the following problem recursively. 我应该递归地解决以下问题。

Finally, write transcribe( S ) . 最后,写transcribe( S ) Here is its description: 这是它的描述:

In an incredible molecular feat called transcription, your cells create molecules of messenger RNA that mirror the sequence of nucleotides in your DNA. 在一个叫做转录的令人难以置信的分子专长中,你的细胞会产生信使RNA分子,反映你DNA中的核苷酸序列。 The RNA is then used to create proteins that do the work of the cell. 然后将RNA用于产生进行细胞工作的蛋白质。 Write a recursive function transcribe( S ) , which should take as input a string S , which will have DNA nucleotides (capital letter As, Cs, Gs, and Ts). 写一个递归函数transcribe( S ) ,它应该把字符串S作为输入,它将具有DNA核苷酸(大写字母As,Cs,Gs和Ts)。 There may be other characters, too, though they will be ignored by your transcribe function—these might be spaces or other characters that are not really DNA nucleotides. 虽然它们会被转录功能忽略,但可能还有其他字符 - 这些字符可能是空格或其他不是DNA核苷酸的字符。

Then, transcribe should return as output the messenger RNA that would be produced from that string S. The correct output simply uses replacement: 然后,转录应该返回将从该字符串S生成的信使RNA作为输出。正确的输出只使用替换:

As in the input become Us in the output. 由于在输入中输入成为Us。
Cs in the input become Gs in the output. 输入中的Cs在输出中变为Gs。
Gs in the input become Cs in the output. 输入中的Gs在输出中变为Cs。
Ts in the input become As in the output. 输入中的Ts变为输出中的As。
any other input characters should disappear from the output altogether 任何其他输入字符应该完全从输出中消失

def transcribe( S ):
  tran = '' * len(S)
  if S[0] == 'A':
    tran = tran + 'U' + transcribe( S[1:] )
    return tran
  elif S[0] == 'C':
    tran = tran + 'G' + transcribe( S[1:] )
    return tran
  elif S[0] == 'G':
    tran = tran + 'C' + transcribe( S[1:] )
    return tran
  elif S[0] == 'T':
    tran = tran + 'A' + transcribe( S[1:] )
    return tran
  else:
    return ""

print transcribe('GATTACA')

Trying to index with [0] into an empty string raises an IndexError . 尝试使用[0]索引到空字符串会引发IndexError

You need to test if the string is empty first , as eventually S[1:] will pass an empty sting into transcribe() : 你需要测试,如果字符串是空的第一个 ,作为最终S[1:]会传递一个空蜇到transcribe()

if not S:
    return ''

You should drop the tran string altogether; 你应该完全放弃tran字符串; that is always an empty string. 总是一个空字符串。 '' * <any number> is still '' . '' * <any number>仍然是''

Rather than test each possible letter in a separate if statement, use a dictionary to map one character to another: 而不是在单独的if语句中测试每个可能的字母,而是使用字典将一个字符映射到另一个字符:

mapping = {'A': 'U', 'C': 'G', 'G': 'C', 'T': 'A'}
def transcribe(S):
    if not S:
        return ''
    replacement = mapping.get(S[0], '')  # default is empty
    return replacement + transcribe(S[1:])

Here's something a bit closer to your original: 这里有一些更接近你原来的东西:

def transcribe( S ):
    if S == '':
        return ''
    elif S[0] == 'A':
        return 'U' + transcribe( S[1:] )
    elif S[0] == 'C':
        return 'G' + transcribe( S[1:] )
    elif S[0] == 'G':
        return 'C' + transcribe( S[1:] )
    elif S[0] == 'T':
        return 'A' + transcribe( S[1:] )
    else:
        return transcribe( S[1:] )

print transcribe('GATTACA')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM