Skip to main content
 首页 » 编程设计

python之从二元语法列表中删除一元语法

2024年10月24日12davidwang456

我已经设法从文本文档创建了 2 个列表。第一个是我的二元语法列表:

keywords = ['nike shoes','nike clothing', 'nike black', 'nike white'] 

和停用词列表:

stops = ['clothing','black','white'] 

我想从我的关键字列表中删除停靠点。使用上面的例子,我得到的输出应该是这样的:

new_keywords = ['nike shoes','nike', 'nike', 'nike'] --> eventually I'd like to remove those dupes.  

这是我到目前为止所做的:

keywords = open("keywords.txt", "r") 
new_keywords = keywords.read().split(",") 
stops = open("stops.txt","r") 
new_stops = stops.read().split(",") 
[i for i in new_keywords if i not in new_stops] 

我遇到的问题是它正在寻找 2 个单词组合而不是单个单词停止....

请您参考如下方法:

您可以分步进行。首先定义一个辅助函数:

def removeStop(bigram, stops): 
    return ' '.join(w for w in bigram.split() if not w in stops) 

然后:

[removeStop(i,new_stops) for i in new_keywords]