擅长:python、mysql、java
<pre><code>from nltk.corpus import stopwords
stop_words = {w.lower() for w in stopwords.words('english')}
sent = [('include', 'details', 'about', 'your', 'performance'),
('show', 'the', 'results,', 'which', 'you\'ve', 'got')]
</code></pre>
<p>如果要创建单个单词列表,但不包含停止词</p>
<pre><code>>>> no_stop_words = [word for sentence in sent for word in sentence if word not in stop_words]
['include', 'details', 'performance', 'show', 'results,', 'got']
</code></pre>
<p>如果你想保持句子完整</p>
<pre><code>>>> sent_no_stop = [[word for word in sentence if word not in stop_words] for sentence in sent]
[['include', 'details', 'performance'], ['show', 'results,', 'got']]
</code></pre>
<p>然而,大多数情况下,你会使用一个单词列表(没有括号)</p>
<pre><code>sent = ['include', 'details', 'about', 'your performance','show', 'the', 'results,', 'which', 'you\'ve', 'got']
>>> no_stopwords = [word for word in sent if word not in stop_words]
['include', 'details', 'performance', 'show', 'results,', 'got']
</code></pre>