Replace multiple elements in string with str methods

后端 未结 2 653
清歌不尽
清歌不尽 2021-01-07 14:04

I am trying to write a function that takes a string of DNA and returns the compliment. I have been trying to solve this for a while now and looked through the Python documen

2条回答
  •  广开言路
    2021-01-07 14:57

    For a problem like this, you can use string.maketrans (str.maketrans in Python 3) combined with str.translate:

    import string
    table = string.maketrans('CGAT', 'GCTA')
    print 'GCTTAA'.translate(table)
    # outputs CGAATT
    

提交回复
热议问题