0

我刚开始学习 Python,我陷入了这个问题:我有一个 DNA 序列,我需要返回它的互补序列。例如,如果我有ATTGCA,它应该返回TAACGT。也就是说,替换ATT、和。A_ 这是一个练习,我不应该使用字符串方法。到目前为止我尝试过的所有事情,都给我“T”作为答案。似乎它只识别第一个字母,然后停止。我该怎么做?CGGC

我试过了:

>>> def get_complementary_sequence(dna):
    for char in dna:
        if char == 'A':
            return 'T'
        elif char == 'T':
            return 'A'
        elif char == 'C':
            return 'G'
        elif char == 'G':
            return 'C'


>>> get_complementary_sequence('ATTGCA')
'T'

并且还尝试过:

def get_complementary_sequence(dna):
    sequence = ""
    for nucleotide in dna:
        if nucleotide == 'A':
            return sequence + 'T'
        elif nucleotide == 'T':
            return sequence + 'A'
        elif nucleotide == 'C':
            return sequence + 'G'
        elif nucleotide == 'G':
            return sequence + 'C'
        return sequence


>>> get_complementary_sequence('ATTGCA')
'T'
4

5 回答 5

5

试试字典,这样你也不需要if's和elif's:

In [45]: dic={'A':'T','T':'A','C':'G','G':'C'}

In [46]: strs="ATTGCA"

In [47]: ''.join(dic[x] for x in strs)
Out[47]: 'TAACGT'

或使用map()

In [52]: ''.join(map(dic.get,strs))
Out[52]: 'TAACGT'
于 2012-10-18T20:13:57.867 回答
3

字符串也是一个字符序列,因此您可以遍历它:

for char in sequence:
于 2012-10-18T20:10:34.933 回答
2

使用字典的另一种方式,完全没有字符串方法:)

trans = {'A':'T','T':'A','C':'G','G':'C'}
with open("temp.txt", "w") as outfile:
    for character in mystring:
        outfile.write(trans[character])
with open("temp.txt") as infile:
    result = infile.read()
于 2012-10-18T20:38:18.180 回答
2

这是 Ashwini Chaudhary 的优雅解决方案的等价物,对于初学者来说可能更容易理解:

complements = {'A': 'T',
               'T': 'A',
               'C': 'G',
               'G': 'C'}
dna_sequence = 'ATTGCA'
new_sequence = []
for char in dna_sequence:
    new_sequence.append(complements[char])
# new_sequence is now ['T', 'A', 'A', 'C', 'G', 'T']
result = ''.join(new_sequence) # result in 'TAACGT'
于 2012-10-18T20:21:19.517 回答
1

这个解决方案很容易理解。希望能帮助到你。

def get_complement(nucleotide):
    """ (str) ->str
    """

    sequence = ""
    for char in nucleotide:
        if char == "A":
            sequence = sequence + "T"
        if char == "T":
            sequence = sequence + "A"
        if char == "G":
            sequence = sequence + "C"
        if char == "C":
            sequence = sequence + "G"

    return sequence
于 2013-09-12T05:52:11.050 回答