Python string replacement

I am trying to replace the appearance of a word with another:

word_list = { "ugh" : "disappointed"}

tmp = ['laughing ugh']

for index, data in enumerate(tmp):
    for key, value in word_list.iteritems():
        if key in data:
            tmp[index]=data.replace(key, word_list[key])

print tmp

While this works ... the appearance ughin is laughingalso replaced by the output:ladisappointeding disappointed.

How to avoid this so that there is a way out laughing disappointed?

+4
source share
6 answers

In this case, you may want to replace word by word.

Example:

word_list = { "ugh" : "disappointed"}
tmp = ['laughing ugh']

for t in tmp:
    words = t.split()
    for i in range(len(words)):
        if words[i] in word_list.keys():
            words[i] = word_list[words[i]]
    newline = " ".join(words)
    print(newline)

Output:

laughing disappointed

Step by Step Explanations:

  • Get every sentence in tmp list:

    for t in tmp:
    
  • divide the sentence into words:

    words = t.split()
    
  • check if wordin wordsin word_list keys. If so, replace it with value:

    for i in range(len(words)):
        if words[i] in word_list.keys():
            words[i] = word_list[words[i]]
    
  • append the replaced words and type the result:

    newline = " ".join(words)
    print(newline)
    
+4
source

You can do this using RegEx:

>>> import re
>>> re.sub(r'\bugh\b', 'disappointed', 'laughing ugh')
'laughing disappointed'

\b .

+3

re.sub:

for key, value in word_list.items():
    tmp = re.sub("\\b{}\\b".format(key), value, tmp[index])
+1
word_list = { "ugh" : "disappointed", "123" : "lol"}
tmp = ['laughing 123 ugh']

for word in tmp:
    words = word.split()
for i in words[:]:
    if  i in word_list.keys():
    replace_value = word_list.get(i)
    words[words.index(i)] = replace_value
output = " ".join(words)
print output

This code will replace each dict key (so the word you want to replace) with the dict value of this key (the word you want to replace) in each case and with several meanings!

Output:
    laughing lol disappointed

Hope this helps!

+1
source

You can use regular expressions:

import re

for index, data in enumerate(tmp):
    for key, value in word_list.iteritems():
        if key in data:
            pattern = '\b' + key + '\b'
            data = re.sub(pattern, value, data)
            tmp[index] = data

Note: you need a string data = ...(to overwrite a variable data), otherwise it will not work correctly if it word_listcontains several records.

0
source

Quickly:

>>> [re.sub(r'\w+', lambda m: word_list.get(m.group(), m.group()), t) 
     for t in tmp]
['laughing disappointed']
>>> 

Very fast:

>>> [re.sub(r'\b(?:%s)\b' % '|'.join(word_list.keys()), lambda m: word_list.get(m.group(), m.group()), t) 
...  for t in tmp]
['laughing disappointed']
>>> 
0
source

All Articles