您的位置:首页 > 编程语言 > Python开发

[Python]这次的Python作业

2013-09-17 13:38 411 查看
Python的官网上说:list.index(x):Return the index in the list of the first item whose value is
x. It is anerror if there is no such item.

下面还有一个例子。看了例子,感觉说是这个x之前还有多少位的意思。

自己写了两句话,发现确实就是这样:

>>> x="abcdef"

>>> x.index("c")

2

这次Python的作业里面有个题让在DNA序列1中按照index加入DNA序列2,说是可以用+, in, indexing等等。google了好久,终于找到了一个解决方案:

def insert_sequence (dna1, dna2, num):

    ''' (str. str, int)-> str

    Return a new DNA sequence obtained by inserting 'dna2' into 'dna1' at the given index 'num'

    >>>insert_sequence ('CCGG', 'AT', 2)

    'CCATGG'

    >>>insert_sequence ('ACGTCGAC', 'CGC', 4)

    'ACGTCGCCGAC'

    '''

    return dna1[:num] + dna2 + dna1[num:]

还有一个作业题目是找出给出DNA序列的complementary sequence。暂时没有找到答案。

def get_complementary_sequence (dna):

    

    '''(str)->str

    Return the DNA sequence that is complementary to the given DNA

    sequence 'dna'

    >>>get_complementary_sequence ('CGCCGAAT')

    'GCGGCTTA'

    >>>get_complementary_sequence ('AAGGTCTTA')

    'TTCCAGAAT'

    '''

    for ch in ['A', 'T', 'C', 'G']:

        if ch in dna:

            dna1 = dna.replace('A', 'T').replace('T', 'A').replace('C', 'G').replace('G', 'C')

            return dna1

//好不容易做到这个样子,但是结果好搞笑。第一个例子的结果是:'CCCCCAAA'。看来'A'变成'T'以后,又被变成了'A'。不知道break能不能解决问题。明天再想办法。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: