如何仅选择某些子字符串
从字符串说 dna = 'ATAGGGATAGGGAGAGAGCGATCGAGCTAG' 我得到了子字符串 dna.format = 'ATAGGGATAG','GGGAGAGAG' 我只想打印长度能被3整除的子字符串 怎么办?我使用模数,但它不起作用!
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
仍然没有给我长度可被 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
corrected code
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 ?
im expecting only substrings which has length divisible by three to be printed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为了包含重叠子字符串,我有以下冗长的版本。这个想法是找到所有开始和结束标记并计算它们之间的距离。
显示所有子字符串,包括重叠的子字符串:
For including overlap substrings, I have the following lengthy version. The idea is to find all starting and ending marks and calculate the distance between them.
Show all substrings, including overlapping ones:
您还可以使用正则表达式:
内部大括号一次匹配 3 个字母。
You can also use the regular expression for that:
the inner braces match 3 letters at once.
使用模数是正确的过程。如果它不起作用,那么你就做错了。请提供您的代码示例以便进行调试。
Using modulo is the correct procedure. If it's not working, you're doing it wrong. Please provide an example of your code in order to debug it.
re.findAll() 将返回一个匹配字符串的数组,您需要迭代每个字符串并对这些字符串进行取模以实现您想要的效果。
re.findAll() will return you an array of matching strings, You need to iterate on each of those and do a modulo on those strings to achieve what you want.