0

I have a dictionary as follows.

myfood = {'yummy tim tam': 1, 'tasty chips': 3, 'yummy': 10, 'a loaf of bread': 5}

I also have a set as follows.

myset = {'yummy', 'a', 'tasty', 'of', 'delicious', 'yum'}

Now I want to identify the elements of myset in substrings of myfood and remove them. Hence, my final myfood dictionary should look as follows.

myfood = {'tim tam': 1, 'chips': 3, 'yummy': 10, 'loaf bread':5}

NOTE: I do not want to remove myset elements if they are full strings. E.g., 'yummy': 10 in myfood is not removed as it is not a substring, but a full string.

My current code is as follows.

for word in myfood.keys():
      if word in myset:
           #Do nothing
      else:
          ######Find the substring part and remove it

Please help me.

0

1 Answer 1

1

Use re.sub to replace only keys that are substrings:

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}
Sign up to request clarification or add additional context in comments.

Comments