在python中使用正则表达式检测子字符串
我有一本字典如下在python中使用正则表达式检测子字符串,python,regex,Python,Regex,我有一本字典如下 myfood = {'yummy tim tam': 1, 'tasty chips': 3, 'yummy': 10, 'a loaf of bread': 5} myset = {'yummy', 'a', 'tasty', 'of', 'delicious', 'yum'} myfood = {'tim tam': 1, 'chips': 3, 'yummy': 10, 'loaf bread':5} for word in myfood.keys():
myfood = {'yummy tim tam': 1, 'tasty chips': 3, 'yummy': 10, 'a loaf of bread': 5}
myset = {'yummy', 'a', 'tasty', 'of', 'delicious', 'yum'}
myfood = {'tim tam': 1, 'chips': 3, 'yummy': 10, 'loaf bread':5}
for word in myfood.keys():
if word in myset:
#Do nothing
else:
######Find the substring part and remove it
我还有一套如下
myfood = {'yummy tim tam': 1, 'tasty chips': 3, 'yummy': 10, 'a loaf of bread': 5}
myset = {'yummy', 'a', 'tasty', 'of', 'delicious', 'yum'}
myfood = {'tim tam': 1, 'chips': 3, 'yummy': 10, 'loaf bread':5}
for word in myfood.keys():
if word in myset:
#Do nothing
else:
######Find the substring part and remove it
现在,我想在myfood
的子字符串中识别myset
的元素并将其删除。因此,我最后的myfood
字典应该如下所示
myfood = {'yummy tim tam': 1, 'tasty chips': 3, 'yummy': 10, 'a loaf of bread': 5}
myset = {'yummy', 'a', 'tasty', 'of', 'delicious', 'yum'}
myfood = {'tim tam': 1, 'chips': 3, 'yummy': 10, 'loaf bread':5}
for word in myfood.keys():
if word in myset:
#Do nothing
else:
######Find the substring part and remove it
注意:如果元素是完整字符串,我不想删除myset
元素。例如,'yummy':myfood
中的10不会被删除,因为它不是子字符串,而是完整字符串
我目前的代码如下
myfood = {'yummy tim tam': 1, 'tasty chips': 3, 'yummy': 10, 'a loaf of bread': 5}
myset = {'yummy', 'a', 'tasty', 'of', 'delicious', 'yum'}
myfood = {'tim tam': 1, 'chips': 3, 'yummy': 10, 'loaf bread':5}
for word in myfood.keys():
if word in myset:
#Do nothing
else:
######Find the substring part and remove it
请帮助我。使用
re.sub
仅替换作为子字符串的键:
pat = re.compile(r'|'.join([r'(\s|\b){}\b'.format(x) for x in myset]))
dct = {}
for k, v in myfood.items():
if k not in myset: # exclude full strings
k = pat.sub('', k).strip()
dct[k] = v
print(dct)
# {'yummy': 10, 'loaf bread': 5, 'tim tam': 1, 'chips': 3}