从字符串中删除多个单词的更好方法是什么?

时间:2021-02-27 01:35:10
bannedWord = ['Good','Bad','Ugly']

def RemoveBannedWords(toPrint,database):
    statement = toPrint
    for x in range(0,len(database)):
        if bannedWord[x] in statement:
            statement = statement.replace(bannedWord[x]+' ','')
    return statement

toPrint = 'Hello Ugly Guy, Good To See You.'

print RemoveBannedWords(toPrint,bannedWord)

The output is Hello Guy, To See You. Knowing Python I feel like there is a better way to implement changing several words in a string. I searched up some similar solutions using dictionaries but it didn't seem to fit this situation.

输出是Hello Guy,To See You。了解Python我觉得有更好的方法来实现更改字符串中的多个单词。我使用字典搜索了一些类似的解决方案,但它似乎不适合这种情况。

4 个解决方案

#1


6  

Here's a solution with regex:

这是一个正则表达式的解决方案:

import re

def RemoveBannedWords(toPrint,database):
    statement = toPrint
    pattern = re.compile("\\b(Good|Bad|Ugly)\\W", re.I)
    return pattern.sub("", toPrint)

toPrint = 'Hello Ugly Guy, Good To See You.'

print RemoveBannedWords(toPrint,bannedWord)

#2


10  

I use

bannedWord = ['Good','Bad','Ugly']
toPrint = 'Hello Ugly Guy, Good To See You.'
print ' '.join(i for i in toPrint.split() if i not in bannedWord)

#3


0  

Yet another variation on a theme. If you are going to be calling this a lot, then it is best to compile the regex once:

关于主题的另一种变化。如果你要打电话给这个,那么最好先编译一次正则表达式:

import re

bannedWord = ['Good','Bad','Ugly']
re_banned_words = re.compile(r"\b(" + "|".join(bannedWord) + ")\\W", re.I)

def RemoveBannedWords(toPrint):
    global re_banned_words
    return re_banned_words.sub("", toPrint)

toPrint = 'Hello Ugly Guy, Good To See You.'
print RemoveBannedWords(toPrint)

#4


0  

Slight variation on Ajay's code, when one of the string is a substring of other in the bannedWord list

Ajay的代码略有变化,当其中一个字符串是bannedWord列表中的其他字符串时

bannedWord = ['good', 'bad', 'good guy' 'ugly']

The result of toPrint ='good winter good guy' would be

toPrint ='好冬天好人'的结果将是

RemoveBannedWords(toPrint,database = bannedWord) = 'winter good'

as it will remove good first. A sorting is required wrt length of elements in the list.

因为它将首先删除好。列表中的元素长度需要排序。

import re

def RemoveBannedWords(toPrint,database):
    statement = toPrint
    database_1 = sorted(list(database), key=len)
    pattern = re.compile(r"\b(" + "|".join(database_1) + ")\\W", re.I)
    return pattern.sub("", toPrint + ' ')[:-1] #added because it skipped last word

toPrint = 'good winter good guy.'

print(RemoveBannedWords(toPrint,bannedWord))

#1


6  

Here's a solution with regex:

这是一个正则表达式的解决方案:

import re

def RemoveBannedWords(toPrint,database):
    statement = toPrint
    pattern = re.compile("\\b(Good|Bad|Ugly)\\W", re.I)
    return pattern.sub("", toPrint)

toPrint = 'Hello Ugly Guy, Good To See You.'

print RemoveBannedWords(toPrint,bannedWord)

#2


10  

I use

bannedWord = ['Good','Bad','Ugly']
toPrint = 'Hello Ugly Guy, Good To See You.'
print ' '.join(i for i in toPrint.split() if i not in bannedWord)

#3


0  

Yet another variation on a theme. If you are going to be calling this a lot, then it is best to compile the regex once:

关于主题的另一种变化。如果你要打电话给这个,那么最好先编译一次正则表达式:

import re

bannedWord = ['Good','Bad','Ugly']
re_banned_words = re.compile(r"\b(" + "|".join(bannedWord) + ")\\W", re.I)

def RemoveBannedWords(toPrint):
    global re_banned_words
    return re_banned_words.sub("", toPrint)

toPrint = 'Hello Ugly Guy, Good To See You.'
print RemoveBannedWords(toPrint)

#4


0  

Slight variation on Ajay's code, when one of the string is a substring of other in the bannedWord list

Ajay的代码略有变化,当其中一个字符串是bannedWord列表中的其他字符串时

bannedWord = ['good', 'bad', 'good guy' 'ugly']

The result of toPrint ='good winter good guy' would be

toPrint ='好冬天好人'的结果将是

RemoveBannedWords(toPrint,database = bannedWord) = 'winter good'

as it will remove good first. A sorting is required wrt length of elements in the list.

因为它将首先删除好。列表中的元素长度需要排序。

import re

def RemoveBannedWords(toPrint,database):
    statement = toPrint
    database_1 = sorted(list(database), key=len)
    pattern = re.compile(r"\b(" + "|".join(database_1) + ")\\W", re.I)
    return pattern.sub("", toPrint + ' ')[:-1] #added because it skipped last word

toPrint = 'good winter good guy.'

print(RemoveBannedWords(toPrint,bannedWord))