【问题标题】:Find position of a particular word in a string查找特定单词在字符串中的位置
【发布时间】:2019-05-31 05:01:03
【问题描述】:

我有一个基因列表,我需要确定列表中的基因是否存在于“文章标题”中,如果存在,则在句子中找到基因的开始和结束位置。

开发的代码确实识别基因并检测基因在句子中的位置。但是,我需要帮助找到基因的起始位置和结束位置

doc = tree.getroot()
 for ArticleTitle in doc.iter('ArticleTitle'):
    file1 = (ET.tostring(ArticleTitle, encoding='utf8').decode('utf8'))
    filename = file1[52:(len(file1))]
    Article= filename.split("<")[0]
    # print(Article)
    # print(type(Article))
    title= Article.split()
    gene_list = ["ABCD1","ADA","ALDOB","APC","ARSB","ATAD3B","AXIN2","BLM","BMPR1A","BRAF","BRCA1"] 
    for item in title:
        for item1 in gene_list:
            if item == item1:
                str_title= ' '.join(title)
                print(str_title)
                print("Gene Found: " + item)
                index= title.index(item)
                print("Index of the Gene :" +str(index))

                result = 0
                for char in str_title:
                    result +=1
                print(result)

当前输出为:

Healthy people 2000: a call to action for ADA members.
Gene Found: ADA
Index of the Gene :8
54

预期输出是:

Healthy people 2000: a call to action for ADA members.
Gene Found: ADA
Index of the Gene :8
Gene start position: 42
Gene End postion:  45

开始和结束位置也应该计算单词之间的空格。

【问题讨论】:

  • 您必须解析文档并列出每个单词的起点,即索引值。那么你可以这样做
  • 你可以使用index方法,但是如果你必须完全匹配这个词我建议你看看regex
  • @DanielMesejo 这对我有帮助!我可以得到结束和开始的位置!谢谢

标签: python python-3.x string split


【解决方案1】:

我们也可以使用 Flashtext

from flashtext import KeywordProcessor

kpo = KeywordProcessor(case_sensitive=True)

gene_list = ["ABCD1","ADA","ALDOB","APC","ARSB","ATAD3B","AXIN2","BLM","BMPR1A","BRAF","BRCA1"] 

for word in gene_list:
    kpo.add_keyword(word)

kpo.extract_keywords("Healthy people 2000: a call to action for ADA members.",span_info=True)
#o/p --> [('ADA', 42, 45)]

【讨论】:

    【解决方案2】:

    可以使用正则表达式

    l=["ABCD1","ADA","ALDOB","APC","ARSB"]
    l='|'.join(l)
    test_string='Healthy people 2000: a call to action for ADA members.'
    pos=0
    for i in test_string.split():
        m=re.search(l,i)
        if m:
            gene=m.group(0)
            start=test_string.find(gene)
            end=start+len(gene)
            print(start,end,gene,pos)
        pos+=1
    

    输出

    (42, 45, 'ADA', 8)
    

    在字符串中没有实际位置的较短解决方案可能是

    l=["ABCD1","ADA","ALDOB","APC","ARSB"]
    l='|'.join(l)
    test_string='Healthy people 2000: a call to action for ADA members.'
    
    [(m.start(),m.group(0),m.end()) for m in re.finditer(l,test_string)]
    

    【讨论】:

    • 匹配对象 (m) 有 start(), end() 方法
    • @DanielMesejo 是的,我知道,但我将字符串作为列表进行迭代,因此一次匹配一个单词。 m.start() 总是给我 0。我的另一个建议是使用 re.finditer 但我不认为这会给出这里需要的确切输出
    • 我明白了,你可以在正则表达式中使用单词边界来避免空格分割
    • 嗯,我看不到如何用原始字符串映射键和值。如果找不到基因,字典可能会因为长字符串而爆炸,而且也会爆炸。您介意张贴作为答案吗?
    • @mad_ 该代码适用于上述测试字符串。然而,对于以下测试字符串:PAH-α-KG 逆向转运刺激了离体蛇肾小管中 PAH 的摄取和净分泌。该基因是索引 3 中的“PAH”,但是,代码检测到“PAH”的位置为 0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多