Easiest Way to Replace a String Using a Dictionary of Replacements

Easiest way to replace a string using a dictionary of replacements?

Using re:

import re

s = 'Спорт not russianA'
d = {
'Спорт':'Досуг',
'russianA':'englishA'
}

pattern = re.compile(r'\b(' + '|'.join(d.keys()) + r')\b')
result = pattern.sub(lambda x: d[x.group()], s)
# Output: 'Досуг not englishA'

This will match whole words only. If you don't need that, use the pattern:

pattern = re.compile('|'.join(d.keys()))

Note that in this case you should sort the words descending by length if some of your dictionary entries are substrings of others.

How to replace words in a string using a dictionary mapping

Here is one way.

a = "you don't need a dog"

d = {"don't": "do not" }

res = ' '.join([d.get(i, i) for i in a.split()])

# 'you do not need a dog'

Explanation

  • Never name a variable after a class, e.g. use d instead of dict.
  • Use str.split to split by whitespace.
  • There is no need to wrap str around values which are already strings.
  • str.join works marginally better with a list comprehension versus a generator expression.

Replace a string using dictionary - regex

Replace the word boundaries with lookarounds that check for any word chars around the search phrase

pattern = re.compile(r'(?<!\w)(' + '|'.join(re.escape(key) for key in dictionary.keys()) + r')(?!\w)')

See the Python demo

The (?<!\w) negative lookbehind fails the match if there is a word char immediately to the left of the current location and the (?!\w) negative lookahead fails the match if there is a word char immediately to the right of the current location.

Replace (?<!\w) with (?<!\S) and (?!\w) with (?!\S) if you need to only match search phrases in between whitespace chars and start/end of string.

Replacing all instances of substring using a dictionary

The second argument to re.sub (the replacement for any matches) can be a callable. If so, it's called with a single argument, the match object, for each match, and its result is substituted into the string. So you can do something like:

d = {'a': 'A', 'b': 'B'}
s = '#a #b and #c'
def replace_it(m):
return d.get(m.group('key'), m.group(0))
print re.sub('#(?P<key>[a-zA-Z]+)', replace_it, s)

Replacing words inside the string with that of dictionary without using any kind of replace function [closed]

you're there. I'd suggest if instead of listing all characters in the sentence you can split() it with # delimiter, the solution becomes a lot simpler.

'$'.join([toReplace[k] if k in toReplace.keys() else k for k in string.split('#')])
# 'Hi. My name is $xyz$. I am from $abc$. My company is $ttl$.'
  • string.split('#') will generate a list of strings, broken at every '#'
  • The list comprehension will search for the list's strings in the dict's keys, and if found, it will pull the item from the dictionary
  • The resulting list after the comprehension can then be joined using the '#' delimiter as it was split. You could also update the join string to be '$'.


Related Topics



Leave a reply



Submit