[Python]这次的Python作业

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能不能解决问题。明天再想办法。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值