Python3替换使用字典

use*_*080 3 python string replace python-3.x

谁能解释一下这里有什么问题:

def get_complementary_sequence(string):
    dic = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
    for a, b in dic.items():
        string = string.replace(a, b)
    return string
Run Code Online (Sandbox Code Playgroud)

我得到'T'和'C'的正确结果,但'A'和'C'不会取代.真的卡住了.

字符串看起来像'ACGTACG'.

Mar*_*ers 6

You are first replacing all As with Ts before then replacing all Ts with As again (including those you just replaced As with!):

>>> string = 'ACGTACG'
>>> string.replace('A', 'T')
'TCGTTCG'
>>> string.replace('A', 'T').replace('T', 'A')
'ACGAACG'
Run Code Online (Sandbox Code Playgroud)

Use a translation map instead, fed to str.translate():

transmap = {ord('A'): 'T', ord('C'): 'G', ord('T'): 'A', ord('G'): 'C'}
return string.translate(transmap)
Run Code Online (Sandbox Code Playgroud)

The str.translate() method requires a dictionary mapping codepoints (integers) to replacement characters (either a single character or a codepoint), or None (to delete the codepoint from the input string). The ord() function gives us those codepoints for the given 'from' letters.

This looks up characters in string, one by one in C code, in the translation map, instead of replacing all As followed by all Ts.

str.translate() has the added advantage of being much faster than a series of str.replace() calls.

Demo:

>>> string = 'ACGTACG'
>>> transmap = {ord('A'): 'T', ord('C'): 'G', ord('T'): 'A', ord('G'): 'C'}
>>> string.translate(transmap)
'TGCATGC'
Run Code Online (Sandbox Code Playgroud)