这是我对 rosalind 项目问题的解决方案。
def prot(rna):
for i in xrange(3, (5*len(rna))//4+1, 4):
rna=rna[:i]+','+rna[i:]
rnaList=rna.split(',')
bases=['U','C','A','G']
codons = [a+b+c for a in bases for b in bases for c in bases]
amino_acids = 'FFLLSSSSYY**CC*WLLLLPPPPHHQQRRRRIIIMTTTTNNKKSSRRVVVVAAAADDEEGGGG'
codon_table = dict(zip(codons, amino_acids))
peptide=[]
for i in range (len (rnaList)):
if codon_table[rnaList[i]]=='*':
break
peptide+=[codon_table[rnaList[i]]]
output=''
for i in peptide:
output+=str(i)
return output
如果我运行prot('AUGGCCAUGGCGCCCAGAACUGAGAUCAAUAGUACCCGUAUUAACGGGUGA')
,我会得到正确的输出'MAMAPRTEINSTRING'
。但是,如果 rna 的序列(输入字符串)是数百个核苷酸(字符)长,我得到一个错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 11, in prot
KeyError: 'CUGGAAACGCAGCCGACAUUCGCUGAAGUGUAG'
你能指出我哪里出错了吗?