我有几行文本,想删除任何带有特殊字符或固定给定字符串的单词(在 python 中)。
例子:
in_lines = ['this is go:od',
'that example is bad',
'amp is a word']
# remove any word with {'amp', ':'}
out_lines = ['this is',
'that is bad',
'is a word']
我知道如何从给定的列表中删除单词,但不能删除带有特殊字符或少数字母的单词。请告诉我,我会添加更多信息。
这是我用于删除选定单词的内容:
def remove_stop_words(lines):
stop_words = ['am', 'is', 'are']
results = []
for text in lines:
tmp = text.split(' ')
for stop_word in stop_words:
for x in range(0, len(tmp)):
if tmp[x] == stop_word:
tmp[x] = ''
results.append(" ".join(tmp))
return results
out_lines = remove_stop_words(in_lines)
慕桂英4014372
FFIVE
相关分类