从字母组合列表中删除字母组合 [英] Remove uni-grams from a list of bi-grams

查看:129
本文介绍了从字母组合列表中删除字母组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法从文本文档中创建了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 ']

这篇关于从字母组合列表中删除字母组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆