【问题标题】:How to get the position of each character in string with python?如何用python获取字符串中每个字符的位置?
【发布时间】: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 in iter]
  • 请参阅stackoverflow.com/questions/2674391/…,了解如何做到这一点。
  • 是的,如果stackoverflow.com/a/16360404/3832970 回答了您的问题,请告知。

标签: regex python-3.x string


【解决方案1】:

对于多个匹配项及其开始和结束索引,请改用finditer

matches = re.finditer(motif, Seq.upper())

for match in matches:
  string_matched = match[0]
  start_index = match.start(0)
  end_index = match.end(0)

【讨论】:

  • 谢谢!但它的抛出错误为ValueError: If using all scalar values, you must pass an index
  • @Kay 这是熊猫错误,您没有提到如何在数据框中使用上述内容。
  • binding.append(string_matched) start.append(start_index) end.append(end_index) dataframe = pd.DataFrame({ 'binding':binding, 'start':start, 'end':end}) dataframe.head()
  • 感谢以上,我正在创建一个匹配和索引列表,然后将它们放在数据框中。
猜你喜欢
  • 2012-10-22
  • 1970-01-01
  • 2016-03-06
  • 2023-03-18
  • 1970-01-01
  • 2018-03-12
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
相关资源
最近更新 更多