Simultaneous .replace functionality

后端 未结 4 1406
一生所求
一生所求 2020-12-19 06:52

I have already converted user input of DNA code (A,T,G,C) into RNA code(A,U,G,C). This was fairly easy

RNA_Code=DNA_Code.replace(\'         


        
4条回答
  •  死守一世寂寞
    2020-12-19 07:33

    Use a translation table:

    RNA_compliment = {
        ord('A'): 'U', ord('U'): 'A',
        ord('G'): 'C', ord('C'): 'G'}
    
    RNA_Code.translate(RNA_compliment)
    

    The str.translate() method takes a mapping from codepoint (a number) to replacement character. The ord() function gives us a codepoint for a given character, making it easy to build your map.

    Demo:

    >>> RNA_compliment = {ord('A'): 'U', ord('U'): 'A', ord('G'): 'C', ord('C'): 'G'}
    >>> 'AUUUGCGGCAAA'.translate(RNA_compliment)
    'UAAACGCCGUUU'
    

提交回复
热议问题