2015-12-18 77 views
2
def complement(dna): 
    transtable = dna.maketrans('acgtnACGTN', 'tgcanTGCAN') 
    return dna.translate(transtable) 

import string 
dna = raw_input("Enter DNA sequence: ") 
print "Reverse Complement is: ", complement(dna) 

我已經檢查了dir(string)並且沒有maketrans。 有沒有辦法導入maketrans?AttributeError:'str'對象沒有屬性'maketrans'

+1

它需要調用maketrans函數。 '從字符串導入maketrans' –

+0

它取決於你使用什麼版本的python。 –

+0

'transtable = string.maketrans('acgtnACGTN','tgcanTGCAN')'在python 2.7上磨損 – The6thSense

回答

5

這適用於Python 3.4:

def complement(dna): 
    transtable = dna.maketrans('acgtnACGTN', 'tgcanTGCAN') 
    return dna.translate(transtable) 

print(complement('TGA')) 

這對於Python 2.7版:

from __future__ import print_function 
import string 

def complement(dna): 
    transtable = string.maketrans('acgtnACGTN', 'tgcanTGCAN') 
    return dna.translate(transtable) 

print(complement('TGA')) 

這可以爲您運行腳本的主要的Python版本:

import sys 

print(sys.version_info.major) 
+0

用()在2,7打印(「你讓我感覺不舒服」) –

+2

@AleksanderGordienko如果你總是從__future__導入print_function'仍然需要Python 2.更新了我的答案。 –

+0

還想一想[unicode_literals](http://python-future.org/unicode_literals.html) – MKesper