首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在文本列表上返回多个匹配?

如何在文本列表上返回多个匹配?
EN

Stack Overflow用户
提问于 2021-12-13 16:16:20
回答 1查看 50关注 0票数 0

我现在有一个函数,它产生一个词和它发生在其中的句子。此时,函数只从术语列表中检索第一个匹配项。我希望能够检索所有的匹配,而不仅仅是第一次。

例如,list_of_matches = ["heart attack", "cardiovascular", "hypoxia"]和一个句子将是text_list = ["A heart attack is a result of cardiovascular...", "Chronic intermittent hypoxia is the..."]

理想的产出是:

代码语言:javascript
复制
['heart attack', 'a heart attack is a result of cardiovascular...'],
['cardiovascular', 'a heart attack is a result of cardiovascular...'],
['hypoxia', 'chronic intermittent hypoxia is the...']
代码语言:javascript
复制
# this is the current function
def find_word(list_of_matches, line):
    for words in list_of_matches:
        if any([words in line]):
            return words, line

# returns list of 'term, matched string'
key_vals = [list(find_word(list_of_matches, line.lower())) for line in text_list if 
find_word(list_of_matches, line.lower()) != None]

# output is currently 
['heart attack', 'a heart attack is a result of cardiovascular...'],
['hypoxia', 'chronic intermittent hypoxia is the...']
代码语言:javascript
复制
EN

回答 1

Stack Overflow用户

发布于 2021-12-13 16:31:32

你会想在这里使用regex。

代码语言:javascript
复制
import re

def find_all_matches(words_to_search, text):
    matches = []
    for word in words_to_search:
        matched_text = re.search(word, text).group()
        matches.append(matched_text)
    return [matches, text]

请注意,这将返回所有匹配的嵌套列表。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70337701

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档