【发布时间】:2018-03-04 16:54:30
【问题描述】:
我正在通过 Pandas 导入一个 csv 文件,格式如下:
test = [
('the beer was good.', 'pos'),
('I do not enjoy my job', 'neg'),
("I ain't feeling dandy today.", 'neg'),
("I feel amazing!", 'pos'),
('Gary is a friend of mine.', 'pos'),
("I can't believe I'm doing this.", 'neg')
]
我想检查停止列表中的任何单词是否包含在定义的测试集中,如果是,请删除它们。但是,在尝试执行此操作时,我只是简单地返回完整列表而没有任何更改。这是我当前的代码:
df = pd.read_csv('test.csv', delimiter=',')
tlist = [tuple(x) for x in df.values]
tlist = [(x.lower(), y.lower()) for x,y in tlist]
def remove_stopwords(train_list):
new_list = []
for word in train_list:
if word not in stopwords.words('english'):
new_list.append(word)
print new_list
remove_stopwords(tlist)
我正在尝试使用 NLTK 语料库提供的停用词。就像我说的那样,当我使用 print(new_list) 测试这段代码时,所发生的一切就是我恢复了 tlist 集。
【问题讨论】:
-
为什么 new_list 是全局的?还有为什么忽略remove_stopwords的返回值?
-
使用python的索引函数来检查一个元素是否存在于列表或元组中
-
@FooBar 抱歉,复制了一些我正在测试的代码以检查某些内容。相应更新。和 bigbounty 你是什么意思?你能进一步解释一下吗?
-
请提供所需的输出。
标签: python pandas csv tuples nltk