所以很喜欢这个问题,只是我想替换的任何部分words
,在任何比赛error_list
用''
。
error_list = ['[br]', '[ex]', 'Something']
words = ['how', 'much[ex]', 'is[br]', 'the', 'fish[br]', 'noSomething', 'really']
所需的输出将是
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']
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句