如何仅选择某些子字符串 [英] How to select only certain Substrings

查看:88
本文介绍了如何仅选择某些子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从一个字符串说dna ='ATAGGGATAGGGAGAGAGAGCGATCGAGCTAG' 我得到了子字符串说dna.format ='ATAGGGATAG','GGGAGAGAG' 我只想打印长度可被3整除的子字符串 怎么做?即时通讯使用模,但无法正常工作!

from a string say dna = 'ATAGGGATAGGGAGAGAGCGATCGAGCTAG' i got substring say dna.format = 'ATAGGGATAG','GGGAGAGAG' i only want to print substring whose length is divisible by 3 how to do that? im using modulo but its not working !

import re
if mydna = 'ATAGGGATAGGGAGAGAGCAGATCGAGCTAG'
print re.findall("ATA"(.*?)"AGA" , mydna)
if len(mydna)%3 == 0
   print mydna

更正后的代码

import re
mydna = 'ATAGGGATAGGGAGAGAGCAGATCGAGCTAG'
re.findall("ATA"(.*?)"AGA" , mydna.format)
if len(mydna.format)%3 == 0:
   print mydna.format

这仍然不能为我提供长度可被三整除的子字符串. .知道有什么问题吗?

this still doesnt give me substring with length divisible by three . . any idea whats wrong ?

我希望只打印长度可被3整除的子字符串

im expecting only substrings which has length divisible by three to be printed

推荐答案

您还可以为此使用正则表达式:

You can also use the regular expression for that:

re.findall('ATA((...)*?)AGA', mydna)

内部括号一次匹配3个字母.

the inner braces match 3 letters at once.

这篇关于如何仅选择某些子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆