简体   繁体   English

将单词列表中的所有单词替换为python中的另一个单词

[英]Replace all words from word list with another string in python

I have a user entered string and I want to search it and replace any occurrences of a list of words with my replacement string. 我有一个用户输入的字符串,我想搜索它并用我的替换字符串替换任何出现的单词列表。

import re

prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]


# word[1] contains the user entered message
themessage = str(word[1])    
# would like to implement a foreach loop here but not sure how to do it in python
for themessage in prohibitedwords:
    themessage =  re.sub(prohibitedWords, "(I'm an idiot)", themessage)

print themessage

The above code doesn't work, I'm sure I don't understand how python for loops work. 上面的代码不起作用,我确定我不明白python for循环是如何工作的。

You can do that with a single call to sub : 你可以通过一次调用sub来做到这一点:

big_regex = re.compile('|'.join(map(re.escape, prohibitedWords)))
the_message = big_regex.sub("repl-string", str(word[1]))

Example: 例:

>>> import re
>>> prohibitedWords = ['Some', 'Random', 'Words']
>>> big_regex = re.compile('|'.join(map(re.escape, prohibitedWords)))
>>> the_message = big_regex.sub("<replaced>", 'this message contains Some really Random Words')
>>> the_message
'this message contains <replaced> really <replaced> <replaced>'

Note that using str.replace may lead to subtle bugs: 请注意,使用str.replace可能会导致细微的错误:

>>> words = ['random', 'words']
>>> text = 'a sample message with random words'
>>> for word in words:
...     text = text.replace(word, 'swords')
... 
>>> text
'a sample message with sswords swords'

while using re.sub gives the correct result: 使用re.sub会得到正确的结果:

>>> big_regex = re.compile('|'.join(map(re.escape, words)))
>>> big_regex.sub("swords", 'a sample message with random words')
'a sample message with swords swords'

As thg435 points out, if you want to replace words and not every substring you can add the word boundaries to the regex: 正如thg435指出的那样,如果你想要替换单词而不是每个子字符串,你可以将单词边界添加到正则表达式:

big_regex = re.compile(r'\b%s\b' % r'\b|\b'.join(map(re.escape, words)))

this would replace 'random' in 'random words' but not in 'pseudorandom words' . 这将取代'random' 'random words' 'random'中的'random words'而不是'pseudorandom words'中的'pseudorandom words'

try this: 试试这个:

prohibitedWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]

themessage = str(word[1])    
for word in prohibitedwords:
    themessage =  themessage.replace(word, "(I'm an idiot)")

print themessage

Code: 码:

prohibitedWords =["MVGame","Kappa","DatSheffy","DansGame",
                  "BrainSlug","SwiftRage","Kreygasm",
                  "ArsonNoSexy","GingerPower","Poooound","TooSpicy"]
themessage = 'Brain'   
self_criticism = '(I`m an idiot)'
final_message = [i.replace(themessage, self_criticism) for i in prohibitedWords]
print final_message

Result: 结果:

['MVGame', 'Kappa', 'DatSheffy', 'DansGame', '(I`m an idiot)Slug', 'SwiftRage',
'Kreygasm', 'ArsonNoSexy', 'GingerPower', 'Poooound','TooSpicy']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果它在单词列表中,则替换字符串中的单词 - Replace a word in a string if it is in a list of words Python:用另一个单词替换字符串中的某些单词? 没有内置功能? - Python: Replace certain words in string with another word? Without built in functions? 如何插入和替换另一个列表中的单词列表或python中的字符串 - how to insert and replace a list of words in another list or a string in python 使用python用列表中的单词替换字符串中的单词 - Replace words in string with words from list using python 如果字符串中的单词属于 pandas 中的单词列表,则替换它 - Replace a word in a string if it belongs to a list of words in pandas 从列表中的一个字符串中搜索任何单词或单词组合(python) - Search for any word or combination of words from one string in a list (python) python - 按序列将单词从一个字符串替换为另一个字符串 - python - replace words from one string to another by the sequence 如何将一个单词替换为列表python中的另一个单词? - How to replace a word to another word in a list python? Python:用单词列表替换句子中的一个单词,并将新句子放在 pandas 的另一列中 - Python: Replace one word in a sentence with a list of words and put thenew sentences in another column in pandas 检查列表中的单词是否在另一个列表 Python 的字符串中 - Check wether words from a list are inside a string of another list Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM