问题描述
所以我正在尝试制作一个简单的程序,将一个短语解码为不同的短语.
so I am trying to make a simple program that will decode one phrase into a different one.
这是我现在的代码
def mRNA_decode(phrase):
newphrase = phrase.replace('A','U'),phrase.replace('T','A'),phrase.replace('C','G'),phrase.replace('G','C')
return newphrase
所以基本上,如果我给字符串 'TATCGTTAATTCGAT'
,所需的输出将是 'AUAGCAAUUAAGCUA'
So basically, if I give the string 'TATCGTTAATTCGAT'
, the desired output would be 'AUAGCAAUUAAGCUA'
相反,我得到 ('TUTCGTTUUTTCGUT', 'AAACGAAAAAACGAA', 'TATGGTTAATTGGAT', 'TATCCTTAATTCCAT')
,这是我输入的短语,但不是将所有更改添加到一个短语中,而是打印四种不同的翻译,每个翻译中只有一个字符发生变化.
instead, I get ('TUTCGTTUUTTCGUT', 'AAACGAAAAAACGAA', 'TATGGTTAATTGGAT', 'TATCCTTAATTCCAT')
, which is the phrase I entered but instead of adding the changes all into one phrase, it prints four different translations all with only one character being changes in each translation.
我将如何更改代码,以便将新短语翻译为具有所需输出的连续字符串,而不是具有四个不同的短语?
How would I change the code so the newphrase is translated so it is on consecutive string with the desired output instead of having four different phrases?
感谢您的帮助
推荐答案
这是因为你每次都用 phrase
变量替换,phrase
的值没有change ,所以你有 4 个不同的输出
it's because you are replacing every time with your phrase
variable , the value of phrase
doesn't change , so you have 4 different outputs
建议,改用翻译函数:
from string import maketrans
intab = "ATCG"
outtab = "UAGC"
trantab = maketrans(intab, outtab)
phrase.translate(trantab)
这篇关于使用.replace替换python中的多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!