从文本列表中删除一组单词

remove a set of words from a list of text

我正在尝试从文本列表中删除单词列表,但输出似乎没有被删除。请帮助我从列表中删除文本

text_list = ['apple is good for health', 'orange and grapes are tasty']
words = ['apple','orange','grapes']
words_format = r'\b(?:{})\b'.format('|',join(words))
remove_words = lambda y: y.replace(words_format,' ')

new_text = list(map(remove_words, text_list))


预期输出:

['is good for health', 'and are tasty']

我会拆分输入,过滤掉无效词,然后再次加入结果:

[" ".join([word for word in text.split(" ") if word not in words]) for text in text_list]

str.replace() doesn't recognize regular expressions. You can use re.sub() 代替。

import re

text_list = ['apple is good for health', 'orange and grapes are tasty']
words = ['apple', 'orange', 'grapes']
words_format = r'\b(?:{})\b'.format('|'.join(words))
remove_words = lambda y: re.sub(words_format, ' ', y)

new_text = list(map(remove_words, text_list))
print(new_text)

输出:

['  is good for health', '  and   are tasty']