我在写这个循环时遇到了问题;它似乎在第二个序列后停止.
我想将互补的DNA序列返回到给定的DNA序列.
例如. (‘AGATTC’) – > (‘TCTAAG’),其中A:T和C:G
def get_complementary_sequence(dna):
"""(str) -> str
> Return the DNA sequence that is complementary to the given DNA sequence
>>> get_complementary_sequence('AT')
('TA')
>>> get_complementary_sequence('AGATTC')
('TCTAAG')
"""
x = 0
complementary_sequence = ''
for char in dna:
complementary_sequence = (get_complement(dna))
return complementary_sequence + (dna[x:x+1])
谁能发现循环不能继续的原因?
以下是我将如何做的示例 – 实际上只有两行代码:
from string import maketrans
DNA="CCAGCTTATCGGGGTACCTAAATACAGAGATAT" #example DNA fragment
def complement(sequence):
reverse = sequence[::-1]
return reverse.translate(maketrans('ATCG','TAGC'))
print complement(DNA)