如何解决使用列表理解删除 Python 列表中的停用词
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 行拆分列表值以创建嵌套列表并将嵌套列表变为一维。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。