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 @ 2022-08-18 17:27  小鲨鱼2018  阅读(101)  评论(0编辑  收藏  举报