我有一本词频如下的词典。你知道吗
mydictionary = {'yummy tim tam':3, 'milk':2, 'chocolates':5, 'biscuit pudding':3, 'sugar':2}
我有一组字符串(去掉标点符号),如下所示。你知道吗
recipes_book = "For todays lesson we will show you how to make biscuit pudding using
yummy tim tam milk and rawsugar"
在上面的字符串中,我只需要通过引用字典输出“biscuit pudding”、“yummy tim tam”和“milk”。不是糖,因为绳子里有生糖。你知道吗
但是,我目前使用的代码也输出了sugar。你知道吗
mydictionary = {'yummy tim tam':3, 'milk':2, 'chocolates':5, 'biscuit pudding':3, 'sugar':2}
recipes_book = "For today's lesson we will show you how to make biscuit pudding using yummy tim tam milk and rawsugar"
searcher = re.compile(r'{}'.format("|".join(mydictionary.keys())), flags=re.I | re.S)
for match in searcher.findall(recipes_book):
print(match)
如何避免使用这样的子字符串,而只考虑一个完整的标记,如“milk”。请帮帮我。你知道吗
使用
re.escape
的另一种方法。 有关re.escape here的更多信息!!!你知道吗输出:
您可以使用regex单词边界更新代码:
输出:
使用单词边界'\b'。简单地说
相关问题 更多 >
编程相关推荐