从 SRE_Match 个对象 (Python) 获取跨度和匹配值
Get span and match values from SRE_Match objects(Python)
我正在尝试在 DNA 序列中找到特定长度(范围为 4 到 12)的匹配项
下面是代码:
import re
positions =[]
for i in range(4,12):
for j in range(len(dna)- i+1):
positions.append(re.search(dna[j:j+i],comp_dna))
#Remove the 'None' from the search
position_hits = [x for x in positions if x is not None]
我明白了:
[<_sre.SRE_Match object; span=(0, 4), match='ATGC'>,.........]
如何从跨度和匹配中提取值?
我试过 .group() 但它抛出了一个错误
AttributeError: 'list' object has no attribute 'group'
如果你想修复当前的方法,你可以使用
position_hits = [x.group() for x in positions if x]
您可以直接在 for
循环中获取所有匹配项:
import re
position_hits = []
for i in range(4,12):
for j in range(len(dna)-i+1):
m = re.search(dna[j:j+i],comp_dna)
position_hits.append(m.group())
我正在尝试在 DNA 序列中找到特定长度(范围为 4 到 12)的匹配项
下面是代码:
import re
positions =[]
for i in range(4,12):
for j in range(len(dna)- i+1):
positions.append(re.search(dna[j:j+i],comp_dna))
#Remove the 'None' from the search
position_hits = [x for x in positions if x is not None]
我明白了:
[<_sre.SRE_Match object; span=(0, 4), match='ATGC'>,.........]
如何从跨度和匹配中提取值? 我试过 .group() 但它抛出了一个错误
AttributeError: 'list' object has no attribute 'group'
如果你想修复当前的方法,你可以使用
position_hits = [x.group() for x in positions if x]
您可以直接在 for
循环中获取所有匹配项:
import re
position_hits = []
for i in range(4,12):
for j in range(len(dna)-i+1):
m = re.search(dna[j:j+i],comp_dna)
position_hits.append(m.group())