我有一个很大的字符串列表,我想检查一个字符串是否出现在一个较大的字符串中。该列表包含一个单词的字符串以及多个单词的字符串。为此,我编写了以下代码:
example_list = ['pain', 'chestpain', 'headache', 'sickness', 'morning sickness']
example_text = "The patient has kneepain as wel as a headache"
emptylist = []
for i in example_text:
res = [ele for ele in example_list if(ele in i)]
emptylist.append(res)
然而,这里的问题是“pain”也被添加到了emptylist中,这是不应该的,因为我只希望在与文本完全匹配的情况下添加示例列表中的内容。我还尝试使用集合:
word_set = set(example_list)
phrase_set = set(example_text.split())
word_set.intersection(phrase_set)
然而,这将“晨吐”分为“晨吐”和“呕吐”。有人知道解决这个问题的正确方法吗
使用PyParsing:
这将产生:
您应该能够使用使用单词边界的正则表达式
这将与
'kneepain'
中的'pain'
不匹配,因为它不是以单词边界开始的。但它会正确地匹配包含空格的子字符串成员们在这篇文章中已经提供了很好的例子
我对疼痛不止一次的匹配文本进行了挑战。我还想了解更多关于比赛地点的信息。我最终得到了以下代码
我写了下面的句子
上面给出了以下输出
相关问题 更多 >
编程相关推荐