如何捕捉一组中最长的序列

编程入门 行业动态 更新时间:2024-10-10 00:25:56
本文介绍了如何捕捉一组中最长的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

任务是找到一组中最长的序列

The task is to find the longest sequence of a group

例如,给定DNA序列: AGATCAGATCTTTTTTCTAATGTCTAGGATATATCAGATCAGATCAGATCAGATCAGATC

for instance, given DNA sequence: "AGATCAGATCTTTTTTCTAATGTCTAGGATATATCAGATCAGATCAGATCAGATCAGATC" and it has 7 occurrences of AGATC. (AGATC) matches all occurrences. Is it possible to write a regular expression that catches only the longest sequence, i.e. AGATCAGATCAGATCAGATCAGATC in the given text? If this is not possible only with regex, how can I iterate through each sequence (i.e. 1st sequence is AGATCAGATC, 2nd - AGATCAGATCAGATCAGATCAGATC et cetera) in python?

推荐答案

使用:

import re sequence = "AGATCAGATCTTTTTTCTAATGTCTAGGATATATCAGATCAGATCAGATCAGATCAGATC" matches = re.findall(r'(?:AGATC)+', sequence) # To find the longest subsequence longest = max(matches, key=len)

说明:

非捕获组(?: AGATC)+

  • + 量词-一次和无限次匹配,例如
  • AGATC 字面上匹配字符AGATC(区分大小写)
  • + Quantifier — Matches between one and unlimited times, as many times as possible.
  • AGATC matches the characters AGATC literally (case sensitive)

结果:

# print(matches) ['AGATCAGATC', 'AGATCAGATCAGATCAGATCAGATC'] # print(longest) 'AGATCAGATCAGATCAGATCAGATC'

您可以测试正则表达式 此处 。

You can test the regex here.

更多推荐

如何捕捉一组中最长的序列

本文发布于:2023-11-30 07:25:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   组中   最长

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!