Python 删除停止字和字符串。标点符号

Python 删除停止字和字符串。标点符号,python,nltk,punctuation,Python,Nltk,Punctuation,我不明白为什么这不起作用: import nltk from nltk.corpus import stopwords import string with open('moby.txt', 'r') as f: moby_raw = f.read() stop = set(stopwords.words('english')) moby_tokens = nltk.word_tokenize(moby_raw) text_no_stop_words_punct

我不明白为什么这不起作用:

import nltk
from nltk.corpus import stopwords
import string

with open('moby.txt', 'r') as f:
    moby_raw = f.read()
    stop = set(stopwords.words('english'))
    moby_tokens = nltk.word_tokenize(moby_raw)
    text_no_stop_words_punct = [t for t in moby_tokens if t not in stop or t not in string.punctuation]

    print(text_no_stop_words_punct)
从输出来看,我有以下几点:

[...';', 'surging', 'from', 'side', 'to', 'side', ';', 'spasmodically', 'dilating', 'and', 'contracting',...]

看来标点符号还在那儿。我做错了什么?

在这一行中,请尝试将“或”改为“和”,这样您的列表将只返回既不是停止词也不是标点符号的单词

text_no_stop_words = [t for t in moby_tokens if t not in stop or t not in string.punctuation]

在这一行中,尝试更改“或”到“和”,这样列表将只返回既不是停止词也不是标点符号的单词

text_no_stop_words = [t for t in moby_tokens if t not in stop or t not in string.punctuation]

它必须是
,而不是

if t not in stop and t not in string.punctuation
或:

或:


后一种解决方案是最快的。

它必须是
,而不是

if t not in stop and t not in string.punctuation
或:

或:

后一种解决方案最快。

关闭。 您需要在比较中使用
而不是
。 如果像“;”这样的标点符号不在
stop
中,那么python不会检查它是否在
字符串中

text_no_stop_words_punct = [t for t in moby_tokens if t not in stop and t not in string.punctuation]
接近。 您需要在比较中使用
而不是
。 如果像“;”这样的标点符号不在
stop
中,那么python不会检查它是否在
字符串中

text_no_stop_words_punct = [t for t in moby_tokens if t not in stop and t not in string.punctuation]