Home > Mobile >  Replace key in a senetence with its value
Replace key in a senetence with its value

Time:09-04

I have a dictionary and a list

dict1= {'good':'bad','happy':'sad','pro':'anti'}
list1=['she is good','this is a product','they are pro choice']
newlist=[]
for index, data in enumerate(list1):
    for key, value in dict1.items():
        if key in data:
            list1[index]=data.replace(key, dict1[key])
            newlist.append(list1[index])
The output I get is
['she is bad','this is a antiduct','they are anti choice']

desired output is
['she is bad','this is a product','they are anti choice']

How can I fix it? It is replacing the pro of prodcut too. I only want it to replace pro when it exists as an independent word.

CodePudding user response:

One option would be to split each phrase and do the substitution with a straight get on the dictionary, rather than a replace:

>>> dict1= {'good':'bad','happy':'sad','pro':'anti'}
>>> list1={'she is good','this is a product','they are pro choice'}
>>> [' '.join(dict1.get(word, word) for word in phrase.split()) for phrase in list1]
['this is a product', 'she is bad', 'they are anti choice']

Note that this will not preserve whitespace in the phrase, nor will it handle punctuation very well. re and itertools.groupby would be useful tools to handle those more complex cases.

CodePudding user response:

Looks like a good use case for a regex with word boundaries:

dict1 = {'good':'bad','happy':'sad','pro':'anti'}
list1 = ['she is good','this is a product','they are pro choice', 'skimming has a basic dimension - product']

import re

regex = fr'\b({"|".join(map(re.escape, dict1))})\b'
out = [re.sub(regex, lambda m: dict1[m.group()], s) for s in list1]

output:

['she is bad',
 'this is a product',
 'they are anti choice',
 'skimming has a basic dimension - product']
  • Related