【发布时间】:2016-01-19 08:18:18
【问题描述】:
我正在尝试从文本字符串列表中删除某些单词(除了使用停用词),但由于某种原因它不起作用
documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]
exclude = ['am', 'there','here', 'for', 'of', 'user']
new_doc = [word for word in documents if word not in exclude]
print new_doc
输出
['Human machine interface for lab abc computer applications', 'A survey of user opinion of computer system response time', 'The EPS user interface management system', 'System and human system engineering testing of EPS', 'Relation of user perceived response time to error measurement', 'The generation of random binary unordered trees', 'The intersection graph of paths in trees', 'Graph minors IV Widths of trees and well quasi ordering', 'Graph minors A survey']
如您所见,EXCLUDE 中的任何词都不会从 DOCUMENTS 中删除(例如,“for”就是一个主要示例)
它与这个运算符一起工作:
new_doc = [word for word in str(documents).split() if word not in exclude]
但是我如何取回 DOCUMENTS 中的初始元素(尽管是“清理过的元素”)?
非常感谢您的帮助!
【问题讨论】:
-
word不是一个词,而是一整行(例如"Human machine interface for lab abc computer applications"),因此永远不会出现在exclude中。 -
@jonrsharpe - 刚刚添加了一个更正,但问题仍然存在(有点不同)
标签: python text stop-words