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 ofdict
. - 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
How to Downgrade Python from 3.7 to 3.5 in Anaconda
How to Substitute Value for a Variable in a Json in Python
Python Pandas: Drop Rows of a Timeserie Based on Time Range
How to Print Float to N Decimal Places Including Trailing 0S
Python Xlsxwriter Set Border Around Multiple Cells
How to Name Dataframes Dynamically in Python
Check If a Specific Class and Value Exist in HTML Using Beautifulsoup Python
How to Select Last Row and Also How to Access Pyspark Dataframe by Index
How to Bold Text in Telepot Telegram Bot
How to Append a List Withoud Adding the Quote
Save Variables in Every Iteration of for Loop and Load Them Later
Python Convert Comma Separated List to Pandas Dataframe
Python Handling Socket.Error: [Errno 104] Connection Reset by Peer
Using a Pandas Dataframe as a Lookup Table
Python: Draw Line Between Two Coordinates in a Matrix
Using Python, How to Access a Shared Folder on Windows Network