如何截取根字符串周围的字符串

2024-06-16 12:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在csv文件中切断根字符串周围的字符串?有什么解决办法吗? 我在另一个专栏的句子中有我的根字符串

例如,我有:

lack of association between the promoter polymorphism of the mtnr1a gene and adolescent idiopathic scoliosis

我的根字符串:mtnr1a,我想要输出:

promoter polymorphism of the mtnr1a gene and adolescent idiopathic

Tags: and文件ofcsvthe字符串句子gene
2条回答

如果您只想在“根字符串”第一次出现的两边各有4个单词,可以使用:

root = 'mtnr1a'
s = 'lack of association between the promoter polymorphism of the mtnr1a gene and adolescent idiopathic scoliosis'
s_list = s.split(' ')
ix = s_list.index(root)

out = ' '.join(s_list[-4+ix:ix+5])
out
# returns:
'promoter polymorphism of the mtnr1a gene and adolescent idiopathic'

将句子转换成单词列表,然后找到根字符串的索引应该可以完成以下工作:

sentence = "lack of association between the promoter polymorphism of the mtnr1a gene and adolescent idiopathic scoliosis"
root = "mtnr1a"

try:
    words = sentence.split()
    n = words.index(root)
    cutoff = ' '.join(words[n-4:n+5])
except ValueError:
    cutoff = None

print(cutoff)

结果:

promoter polymorphism of the mtnr1a gene and adolescent idiopathic

注意:当在句子中找不到根字符串时,需要捕获ValueError。在这种情况下,cutoff被设置为None

相关问题 更多 >