gpt4 book ai didi

python - 属性错误 : 'str' object has no attribute 'maketrans'

转载 作者:行者123 更新时间:2023-11-28 22:39:51 26 4
gpt4 key购买 nike

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?

最佳答案

这适用于 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)

关于python - 属性错误 : 'str' object has no attribute 'maketrans' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34350054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com