3 回答

TA贡献1946条经验 获得超4个赞
如果 score_list 中的所有值都采用以下格式:
344 位 (186),预期 = 5e-91
这个答案不是最漂亮的,但它也将值转换为整数,因为您可能想用它作为生物信息学数据进行分析。
import re
# This is your code
score_list = []
for record in blast_file:
score = re.search(r'Score = (.+\d)', record).group(1)
score_list.append(score)
print(score_list)
# This will extract the bit score
new_list = []
for i in score_list:
new_list.append(re.findall(r'^\d*', i))
new_list = [i for val in new_list for i in val]
new_list = list(map(int, new_list))
new_list
^\d* 将匹配任意数量的数字,直到“位”之前的空格。然后接下来的两行将列表的列表展平并将所有数字从字符串转换为整数。

TA贡献1859条经验 获得超6个赞
下面是你可以获得任何字符串的方法,只需像我一样插入正则表达式。
def new():
string="Score = 344 bits (186), Expect = 5e-91"
n=re.search("= (.*?)\ bits",string)
m=n.group(1)
return str(m)

TA贡献1828条经验 获得超3个赞
使用当前的正则表达式,您将匹配所有字符直到最后一位,然后包括最后一位。
如果您只想匹配数字,请从 更改Score = (.+\d)
为Score = (\d+)
。
另外,请注意等式符号后有双空格。如果你想忽略间距,这将是你的正则表达式:Score\s*=\s*(.+\d)
添加回答
举报