python中实现查找NDA的motif
给定两个字符串str1和str2,返回字符串str2在str1中的所有的位置索引。
001、 直接实现
[root@pc1 test01]# ls test.py [root@pc1 test01]# cat test.py ## 程序 #!/usr/bin/env python # -*- coding: utf-8 -*- str1 = "GATATATGCATATACTT" ## 在str1中查找str2,返回索引 str2 = "ATAT" list1 = list() for i in range(len(str1)): idx = str1.find(str2,i) if idx != -1 and i == idx: list1.append(idx + 1) print(list1) [root@pc1 test01]# python3 test.py ## 结果 [2, 4, 10]
002、函数结构实现
[root@pc1 test01]# ls test.py [root@pc1 test01]# cat test.py ## 程序 #!/usr/bin/env python # -*- coding: utf-8 -*- str1 = "GATATATGCATATACTT" str2 = "ATAT" def motif(x,y): list1 = [] for i in range(len(x)): idx = x.find(y,i) if idx != -1 and idx == i: list1.append(i + 1) return list1 print(motif(str1,str2)) [root@pc1 test01]# python test.py ## 结果 [2, 4, 10]
解法2
003、
[root@pc1 test01]# ls test.py [root@pc1 test01]# cat test.py ## 检索程序 #!/usr/bin/env pyhton # -*- coding: utf-8 -*- str1 = "GATATATGCATATACTT" str2 = "ATAT" list1 = list() idx = str1.find(str2) while idx != -1: list1.append(idx + 1) idx = str1.find(str2, idx + 1) print(list1) [root@pc1 test01]# python3 test.py ## 运行结果 [2, 4, 10]
004、函数结构实现
[root@pc1 test01]# ls test.py [root@pc1 test01]# cat test.py #!/usr/bin/env python # -*- coding: utf-8 -*- str1 = "GATATATGCATATACTT" str2 = "ATAT" def get_idx(x,y): ## 定义函数 list1 = [] idx = x.find(y) while idx != -1: list1.append(idx + 1) idx = x.find(y, idx + 1) return list1 print(get_idx(str1, str2)) [root@pc1 test01]# python3 test.py ## 计算结果 [2, 4, 10]
。
参考:https://mp.weixin.qq.com/s?__biz=MzIxMjQxMDYxNA==&mid=2247484218&idx=1&sn=4ffe37427e4bb7de12b9db984d67a7d1&chksm=9747caa3a03043b5886f4eca3d3d56b4cbb7be20b361de08eeeb5f1b64bbef726d473409b52e&cur_album_id=1635727573621997580&scene=190#rd
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2022-08-30 seurat单细胞数据分析实现 DimHeatmap函数
2022-08-30 R语言中seq函数