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

PythonRegex

Python Problem Overview


Consider..

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

s = 'Спорт russianA'

I'd like to replace all dict keys with their respective dict values in s.

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

You could use the reduce function:

reduce(lambda x, y: x.replace(y, dict[y]), dict, s)

Solution 3 - Python

Solution found here (I like its simplicity):

def multipleReplace(text, wordDict):
    for key in wordDict:
        text = text.replace(key, wordDict[key])
    return text

Solution 4 - Python

one way, without re

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

s = 'Спорт russianA'.split()
for n,i in enumerate(s):
    if i in d:
        s[n]=d[i]
print ' '.join(s)

Solution 5 - Python

Almost the same as ghostdog74, though independently created. One difference, using d.get() in stead of d[] can handle items not in the dict.

>>> d = {'a':'b', 'c':'d'}
>>> s = "a c x"
>>> foo = s.split()
>>> ret = []
>>> for item in foo:
...   ret.append(d.get(item,item)) # Try to get from dict, otherwise keep value
... 
>>> " ".join(ret)
'b d x'

Solution 6 - Python

I used this in a similar situation (my string was all in uppercase):

def translate(string, wdict): for key in wdict: string = string.replace(key, wdict[key].lower()) return string.upper() hope that helps in some way... :)

Solution 7 - Python

With the warning that it fails if key has space, this is a compressed solution similar to ghostdog74 and extaneons answers:

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

s = 'Спорт russianA'

' '.join(d.get(i,i) for i in s.split())

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmeder omuralievView Question on Stackoverflow
Solution 1 - PythonMax ShawabkehView Answer on Stackoverflow
Solution 2 - PythoncodeapeView Answer on Stackoverflow
Solution 3 - PythonChristopheDView Answer on Stackoverflow
Solution 4 - Pythonghostdog74View Answer on Stackoverflow
Solution 5 - PythonextraneonView Answer on Stackoverflow
Solution 6 - Pythonuser2769207View Answer on Stackoverflow
Solution 7 - PythonAnton vBRView Answer on Stackoverflow