Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中使用正则表达式检测子字符串_Python_Regex - Fatal编程技术网

在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}