【发布时间】:2019-12-10 07:42:53
【问题描述】:
如何在python中获取字符串(fasta)中匹配字符(小字符串)的位置?
我正在使用fasta文件作为字符串使用正则表达式'[AGCT][TG][TC][GT]TG'和主题搜索主题,我还想知道并保存主题在字符串中出现的位置。
rdict = dict([ (x[1],x[0]) for x in enumerate(Seq) ])
motif = '[AGCT][TG][TC][GT]TG'
#for match in Seq:
matches = re.findall(motif, Seq.upper())
print(matches)
Seq.index(matches)
上面的代码完成了搜索主题的工作,但只返回一个字符的位置。如何更改它以给出主题的开始到结束位置(小字符串)。
【问题讨论】:
-
如果你知道1个字符的位置,你也知道匹配的长度是6,那么你不能做什么?
-
也许
matches = [x.span() for x in re.finditer(motif, Seq.upper())]? -
iter = re.finditer(motif,Seq.upper()) indices = [m.start(0) for m initer] -
请参阅stackoverflow.com/questions/2674391/…,了解如何做到这一点。
-
是的,如果stackoverflow.com/a/16360404/3832970 回答了您的问题,请告知。
标签: regex python-3.x string