相关疑难解决方法(0)

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

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)
Run Code Online (Sandbox Code Playgroud)

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

python regex string replace python-3.x

5
推荐指数
3
解决办法
6419
查看次数

标签 统计

python ×1

python-3.x ×1

regex ×1

replace ×1

string ×1