从 Python 中的列表中删除停用词列表(自然语言处理)

Remove stopwords list from list in Python (Natural Language Processing)

我一直在尝试使用 python 3 代码删除停用词,但我的代码似乎不起作用,我想知道如何从下面的列表中删除停用词。示例结构如下:

    from nltk.corpus import stopwords

    word_split1=[['amazon','brand','- 
    ','solimo','premium','almonds',',','250g','by','solimo'],
    ['hersheys','cocoa', 'powder', ',', '225g', 'by', 'hersheys'], 
    ['jbl','t450bt','extra','bass','wireless','on- 
    ear','headphones','with','mic','white','by','jbl','and']]

我正在尝试删除停用词并尝试了以下是我的代码,如果有人能帮我解决这个问题,我将不胜感激。这是下面的代码

    stop_words = set(stopwords.words('english'))

    filtered_words=[]
    for i in word_split1:
        if i not in stop_words:
            filtered_words.append(i)

我收到错误:

    Traceback (most recent call last):
    File "<ipython-input-451-747407cf6734>", line 3, in <module>
    if i not in stop_words:
    TypeError: unhashable type: 'list'

你有一个列表列表。

尝试:

word_split1=[['amazon','brand','- ','solimo','premium','almonds',',','250g','by','solimo'],['hersheys','cocoa', 'powder', ',', '225g', 'by', 'hersheys'],['jbl','t450bt','extra','bass','wireless','on-ear','headphones','with','mic','white','by','jbl','and']]
stop_words = set(stopwords.words('english'))
filtered_words=[]
for i in word_split1:
    for j in i:
        if j not in stop_words:
            filtered_words.append(j)

或展平您的列表。

例如:

from itertools import chain    

word_split1=[['amazon','brand','- ','solimo','premium','almonds',',','250g','by','solimo'],['hersheys','cocoa', 'powder', ',', '225g', 'by', 'hersheys'],['jbl','t450bt','extra','bass','wireless','on-ear','headphones','with','mic','white','by','jbl','and']]
stop_words = set(stopwords.words('english'))
filtered_words=[]
for i in chain.from_iterable(word_split1):
    if i not in stop_words:
        filtered_words.append(i)

filtered_words = [i for i in chain.from_iterable(word_split1) if i not in stop_words]

该列表是一个二维数组,您正在尝试散列一个列表,先将其转换为一维数组,然后您的代码才能正常工作,

word_split1 = [j for x in word_split1 for j in x] 

stop_words = set(stopwords.words('english'))

filtered_words=[]
for i in word_split1:
    if i not in stop_words:
        filtered_words.append(i)