问题描述
Python 菜鸟对于简单的问题很抱歉,但我找不到适合我情况的确切解决方案。
我有一个 python 列表,我想从列表中删除停用词。如果与另一个令牌配对,我的代码不会删除停用词。
from nltk.corpus import stopwords
rawData = ['for','the','game','the movie']
text = [each_string.lower() for each_string in rawData]
newText = [word for word in text if word not in stopwords.words('english')]
print(newText)
当前输出: ['游戏','电影']
期望的输出 ['游戏','电影']
我更愿意为此使用列表理解。
解决方法
我花了一段时间才这样做,因为列表推导式不是我的事。无论如何,我就是这样做的:
import functools
stopwords = ["for","the"]
rawData = ['for','the','game','the movie']
lst = functools.reduce(lambda x,y: x+y,[i.split() for i in rawData])
newText = [word for word in lst if word not in stopwords]
print(newText)
基本上,第 4 行拆分列表值以创建嵌套列表并将嵌套列表变为一维。