假设我有一个关键字列表:
keywords = ["history terms","history words","history vocab","history words terms","history vocab words","science list","science terms vocab","math terms words vocab"]
以及主要术语列表:
`main_terms = ["terms","words","vocab","list"]`
已更新以更清楚地说明问题:
我正在制作的脚本是从一长串关键字中删除近似重复项。我已经设法删除了拼写错误和细微的变体(例如“hitsory terms”、“history term”)。
我的问题 是我在此关键字列表中查找多个字词,但在我在关键字中找到其中一个字词后(例如“历史字词” ) 除了具有不同的术语或术语组合(例如“history vocab”、“history words”、“history words terms”等)外,所有相同的关键字都应被视为重复。
- 可以在关键字中包含多个术语(例如“数学术语单词词汇”)只要除了术语数量较少之外没有相同的关键字(例如“数学术语单词”或理想情况下是“数学词汇”等单个术语)。
请您参考如下方法:
遍历 keywords 并根据 main_terms 检查每一个:
keywords = ["history terms",
"history words",
"history vocab",
"history words terms",
"history vocab words",
"science list",
"science terms vocab",
"math terms words vocab"]
main_terms = {"terms","words","vocab","list"}
result = {}
for words in keywords:
s = set(words.split())
s_subject = s - main_terms
subject = s_subject and next(iter(s_subject))
if s | main_terms and subject and subject not in result:
result[subject] = words
将结果值转换为列表:
>>> list(result.values())
['math terms words vocab', 'history terms', 'science list']
