这就是我所拥有的,但似乎有点多余。也许在Python中更有经验的人知道如何清理它?应该是非常自我解释它的作用。
def complementary_strand(self, strand):
''' Takes a DNA strand string and finds its opposite base pair match. '''
strand = strand.upper()
newstrand = ""
for i in range(0, len(strand)):
if strand[i] == "T":
newstrand += "A"
if strand[i] == "A":
newstrand += "T"
if strand[i] == "G":
newstrand += "C"
if strand[i] == "C":
newstrand += "G"
return newstrand
答案 0 :(得分:12)
如果字符串足够长,可能是最有效的方法:
import string
def complementary_strand(self, strand):
return strand.translate(string.maketrans('TAGCtagc', 'ATCGATCG'))
这是使用translate和maketrans方法。您还可以在函数外部移动转换表:
import string
def __init__(self, ...):
self.trans = string.maketrans('TAGCtagc', 'ATCGATCG')
def complementary_strand(self, strand):
return strand.translate(self.trans)
答案 1 :(得分:6)
更好的是制作发电机,而不是:
TRANS = { "T": "A", "A": "T", "G": "C", "C": "G" }
def complementary_strand(self, strand):
for base in strand.upper():
yield TRANS[base]
然后你可以以任何你想要的方式使用它,并且迭代器更有效:
for base in strand.complementary_strand():
# Do something
答案 2 :(得分:3)
像
这样的东西def complementary_strand(self, strand):
return strand.upper().translate(maketrans("TAGC", "ATCG"))
答案 3 :(得分:2)
def __init__(self, *args):
# ... original __init__ method, and:
self.trans = { "T": "A", "A": "T", "G": "C", "C": "G" }
def complementary_strand(self, strand):
'''Takes a DNA strand string and returns its opposite base pair match.'''
return ''.join([self.trans[base] for base in strand.upper()])
答案 4 :(得分:0)
def compliment(Nucleotide):
comp = []
for i in Nucleotide:
if i == "T":
comp.append("A")
if i == "A":
comp.append("T")
if i == "G":
comp.append("C")
if i == "C":
comp.append("G")
return ''.join(comp)