Python结构错误

-1 python string algorithm

我正在编写一个程序,我可以在其中反转序列并将所有的As替换为Ts,所有Cs都用Gs,所有Gs用Cs,所有Ts用As.该程序是读取碱基序列并输出反向互补序列.我无法做到这一点所以任何人都可以通过查看我的代码来帮助我:

word = raw_input("Enter sequence: ")
a = word.replace('A', 'T')
b = word.replace('C', 'G')
c = word.replace('G', 'C')
d = word.replace('T', 'A')
if a == word and b == word and c == word and d == word:
    print "Reverse complement sequence: ", word
Run Code Online (Sandbox Code Playgroud)

我想要这种输出:

Enter sequence: CGGTGATGCAAGG
Reverse complement sequence: CCTTGCATCACCG
Run Code Online (Sandbox Code Playgroud)

问候

DSM*_*DSM 5

我可能会这样做:

word = raw_input("Enter sequence:")

# build a dictionary to know what letter to switch to
swap_dict = {'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C'}

# find out what each letter in the reversed word maps to and then join them
newword = ''.join(swap_dict[letter] for letter in reversed(word))

print "Reverse complement sequence:", newword
Run Code Online (Sandbox Code Playgroud)

我不太明白你的if陈述,但上面的代码避免了通过循环每个字母,决定它应该变成什么,然后组合结果来需要一个.这样每个字母只会转换一次.

编辑:oops,我没注意到你想要反转字符串.固定.