Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python列表中查找并替换为多个字符串值_Python_String_List - Fatal编程技术网

在Python列表中查找并替换为多个字符串值

在Python列表中查找并替换为多个字符串值,python,string,list,Python,String,List,非常类似于此,除了我想用'替换错误列表中与任何内容匹配的单词的任何部分 期望的输出是 words = ['how', 'much', 'is', 'the', 'fish', 'no', 'really'] 我徒劳的尝试是 words = [w.replace(error_list , '') for w in word] 编辑:也许我也应该说,我已经用循环完成了这项工作,但我正在寻找一种更具python风格的方式 error_list = ['[br]', '[ex]', 'Someth

非常类似于此,除了我想用
'
替换
错误列表中与任何内容匹配的
单词的任何部分

期望的输出是

 words = ['how', 'much', 'is', 'the', 'fish', 'no', 'really']
我徒劳的尝试是

words = [w.replace(error_list , '') for w in word]
编辑:也许我也应该说,我已经用循环完成了这项工作,但我正在寻找一种更具python风格的方式

error_list = ['[br]', '[ex]', 'Something']
words = ['how', 'much[ex]', 'is[br]', 'the', 'fish[br]', 'noSomething', 'really']   

for j in error_list:
    for index, i in enumerate(words):
            if(j in i):
                    i1=i.replace(j,"")
                    words[index]= i1
输出

['how', 'much', 'is', 'the', 'fish', 'no', 'really']

用谷歌搜索你的问题的确切标题产生了一些很好的信息(链接的目标是第一次命中)。关于“Python替换多个字符串”的答案肯定会有用,但可能有点太强大了。最终,我将不得不创建一个dict,使用我的列表作为键,使用
作为值。这与我最初使用循环的方法类似,因此应该可以很好地工作+1@josh如果出现这种情况,我们可以跳过
!!欢迎有任何新的方法。。
['how', 'much', 'is', 'the', 'fish', 'no', 'really']