【发布时间】:2018-03-14 07:15:27
【问题描述】:
我有一个单词词典,其频率如下。
mydictionary = {'yummy tim tam':3, 'fresh milk':2, 'chocolates':5, 'biscuit pudding':3}
我有一组字符串如下。
recipes_book = "For today's lesson we will show you how to make biscuit pudding using
yummy tim tam and fresh milk."
在上面的字符串中,我有字典中的“饼干布丁”、“美味的 tim tam”和“新鲜牛奶”。
我目前正在对字符串进行标记以识别字典中的单词,如下所示。
words = recipes_book.split()
for word in words:
if word in mydictionary:
print("Match Found!")
但它只适用于一个单词字典键。因此,我对最快的方法(因为我的真实食谱是非常大的文本)感兴趣,以识别具有多个单词的字典键。请帮帮我。
【问题讨论】:
-
也许re.findall 是您正在寻找的。或者可能是正则表达式库中的一些其他函数。
标签: python