【发布时间】:2022-01-17 02:31:23
【问题描述】:
我目前有一个函数可以产生一个术语和它出现的句子。此时,该函数只是从术语列表中检索第一个匹配项。我希望能够检索所有匹配项,而不仅仅是第一个。
例如,list_of_matches = ["heart attack", "cardiovascular", "hypoxia"]
一个句子是text_list = ["A heart attack is a result of cardiovascular...", "Chronic intermittent hypoxia is the..."]
理想的输出是:
['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...']
# 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...']
【问题讨论】: