以下是一个使用Python编程语言的示例代码,用于保留指定清单中的精确词语,并删除其他词语:
def keep_exact_words(word_list, exact_words):
"""
保留清单中的精确词语,删除其他词语。
:param word_list: 要处理的词语清单
:param exact_words: 要保留的精确词语清单
:return: 处理后的词语清单
"""
return [word for word in word_list if word in exact_words]
# 示例用法
word_list = ["apple", "banana", "carrot", "date"]
exact_words = ["banana", "date"]
filtered_words = keep_exact_words(word_list, exact_words)
print(filtered_words)
在上面的示例中,我们定义了一个名为keep_exact_words
的函数,该函数接受两个参数:word_list
是要处理的词语清单,exact_words
是要保留的精确词语清单。函数使用列表推导式遍历word_list
中的每个词语,只保留出现在exact_words
中的词语。最后,我们使用示例数据进行测试,并打印过滤后的词语清单。
输出结果为:['banana', 'date']
,只保留了清单中出现在exact_words
中的词语。