用python中的另一个字符串替换单词列表中的所有单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15658187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace all words from word list with another string in python
提问by Zac
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 循环是如何工作的。
采纳答案by Bakuriu
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.replacemay 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.subgives 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 wordsand 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'in'random words'但不会替换in 'pseudorandom words'。
回答by Artsiom Rudzenka
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
回答by zen11625
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']

