我有一个字符串列表和一个句子,如下所示:
list_of_strings=["skin allergy","hair loss","allergy","hair", "skin"]
sentence="She experienced skin allergy and hair loss after using it for 2-3 weeks"
我想将list_of_strings
与sentence
匹配,并将输出仅打印为更长的短语(忽略子字符串):
skin allergy
hair loss
我写了这个code:但这会提取所有匹配的内容。
答案 0 :(得分:1)
使用正则表达式。
例如:
import re
list_of_strings=["skin allergy","hair loss","allergy","hair", "skin"]
sentence="She experienced skin allergy and hair loss after using it for 2-3 weeks"
pattern = re.compile(r"(\b" + "|".join(list_of_strings) + r")\b")
m = pattern.findall(sentence)
print(m)
输出:
['skin allergy', 'hair loss']