从字母组合列表中删除字母组合
问题描述:
我设法从文本文档中创建了2个列表.首先是我的字母组合列表:
I have managed to create 2 lists from text documents. The first is my bi-gram list:
keywords = ['nike shoes','nike clothing', 'nike black', 'nike white']
和停用词列表:
stops = ['clothing','black','white']
我想从关键字"列表中删除停止位置".使用上面的示例,我追求的输出应如下所示:
I want to remove the Stops from my Keywords list. Using the above example, the output I am after should look like this:
new_keywords = ['nike shoes','nike', 'nike', 'nike'] --> eventually I'd like to remove those dupes.
这是我到目前为止所做的:
This is what I've done so far:
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个单词组合,而不是单个单词停止....
The problem I am having is that it is looking for the 2 words combos rather than the single word stops....
答
假设您有2个列表,这将满足您的要求:
assuming you have the 2 lists this will do what you want:
new_keywords = []
for k in keywords:
temp = False
for s in stops:
if s in k:
new_keywords.append(k.replace(s,""))
temp = True
if temp == False:
new_keywords.append(k)
这将创建一个您发布的列表:
This will create a list like you posted:
['nike shoes', 'nike ', 'nike ', 'nike ']
要消除双打,请执行以下操作:
To eliminate the doubles do this:
new_keywords = list(set(new_keywords))
所以最终列表如下:
['nike shoes', 'nike ']