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