[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能不能解决问题。明天再想办法。
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能不能解决问题。明天再想办法。
相关文章推荐
- Python第一天课后作业
- Python作业-选课系统
- python第二节作业
- python 教程 第十四章、 地址薄作业
- 利用python编写设计多线程web服务器(计算机网络_自顶向下第六版_第二章1和4的编程作业)
- python作业1
- 从零开始学Python08作业思路:开发简单的FTP
- Python学习day1作业总结
- Python第三天课后作业
- python3 概率论 作业
- Python学习作业之登陆接口_For
- Python作业2.1-2.10 3月8日
- Linux+Python运维培训班第4期--马哥——第3次作业(20170212)
- 作业第二周PYTHON
- Linux system Python_作业三
- python课程学习-模块1-作业-03购物车程序
- 铁乐学python_day09_作业
- 2018.3.7 Python作业
- python从入门到实践第七章的练习题作业