python中实现查找DNA中的motif

 

001、方法1

复制代码
root@PC1:/home/test# ls
test.py
root@PC1:/home/test# cat test.py                     ## 测试程序
#!/usr/bin/python

str1 = "GATATATGCATATACTT"
str2 = "ATAT"

result = []

pos = 0
while True:
    idx = str1.find(str2, pos)
    if idx == -1:
        break
    else:
        result.append(idx + 1)
        pos = idx + 1

print(result)
root@PC1:/home/test# python test.py                ## 执行程序
[2, 4, 10]
复制代码

 

002、方法2

复制代码
root@PC1:/home/test# ls
test.py
root@PC1:/home/test# cat test.py                        ## 测试程序
#!/usr/bin/python

str1 = "GATATATGCATATACTT"
str2 = "ATAT"

result = []

for i in range(len(str1)):
    if str1.find(str2, i) and str1.find(str2, i) not in result and str1.find(str2, i) != -1:
        result.append(str1.find(str2, i))

print([k + 1  for k in result])
root@PC1:/home/test# python test.py                    ## 执行程序
[2, 4, 10]
复制代码

 

方法3:

复制代码
root@PC1:/home/test# ls
test.py
root@PC1:/home/test# cat test.py                         ## 测试程序
#!/usr/bin/python

str1 = "GATATATGCATATACTT"
str2 = "ATAT"

result = []
idx = str1.find(str2)

while idx != -1:
    result.append(idx + 1)
    idx = str1.find(str2, idx + 1)

print(" ".join([str(k) for k in result]))

root@PC1:/home/test# python test.py                  ## 执行程序
2 4 10
复制代码

 

参考:https://mp.weixin.qq.com/s?__biz=MzIxMjQxMDYxNA==&mid=2247484218&idx=1&sn=4ffe37427e4bb7de12b9db984d67a7d1&chksm=9747caa3a03043b5886f4eca3d3d56b4cbb7be20b361de08eeeb5f1b64bbef726d473409b52e&scene=178&cur_album_id=1635727573621997580#rd

 

posted @   小鲨鱼2018  阅读(110)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示