用python中的另一个字符串替换单词列表中的所有单词 [英] Replace all words from word list with another string in python

查看:73
本文介绍了用python中的另一个字符串替换单词列表中的所有单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户输入的字符串,我想搜索它并用我的替换字符串替换任何出现的单词列表.

导入重新disabledWords = ["MVGame","Kappa","DatSheffy","DansGame","BrainSlug","SwiftRage","Kreygasm","ArsonNoSexy","GingerPower","Poooound","TooSpicy"]# word[1] 包含用户输入的消息themessage = str(word[1])# 想在这里实现一个foreach循环,但不知道如何在python中实现对于禁用词中的消息:themessage = re.sub(prohibitedWords, "(我是个白痴)", themessage)打印消息

上面的代码不起作用,我确定我不明白python for循环是如何工作的.

解决方案

您只需调用一次 sub 即可:

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

示例:

<预><代码>>>>进口重新>>>disabledWords = ['Some', 'Random', 'Words']>>>big_regex = re.compile('|'.join(map(re.escape, disabledWords)))>>>the_message = big_regex.sub("<replaced>", '此消息包含一些真正随机的单词')>>>the_message'此消息包含<replaced>真的<替换><替换>'

请注意,使用 str.replace 可能会导致细微的错误:

<预><代码>>>>单词 = ['随机','单词']>>>text = '带有随机单词的示例消息'>>>逐字逐句:... text = text.replace(word, 'swords')...>>>文本'带有 sswords 剑的示例消息'

同时使用 re.sub 给出正确的结果:

<预><代码>>>>big_regex = re.compile('|'.join(map(re.escape, words)))>>>big_regex.sub("swords", '带有随机单词的示例消息')'带有剑剑的示例消息'

正如 thg435 指出的那样,如果您想替换 words 而不是每个子字符串,您可以将单词边界添加到正则表达式中:

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

这将替换'random words'中的'random',但不会替换'pseudorandom words'.

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.

解决方案

You can do that with a single call to 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:

>>> 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:

>>> 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:

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'.

这篇关于用python中的另一个字符串替换单词列表中的所有单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆