【发布时间】:2015-10-07 16:44:19
【问题描述】:
我试图将以下示例放入使用 Python 进行 NLP 的列表理解,第 3 章中的问题 10。我尝试了各种组合来尝试使这种理解发挥作用。我想在该单词的长度旁边显示“已发送”中的单词。
import nltk
sent = sent = ['The', 'dog', 'gave', 'John', 'the', 'newspaper']
result = []
[word_len=(word, len(word)), result.append(word_len) for word in sent]
File "<stdin>", line 1
[word_len = (word, len(word)), result.append(word_len) for word in sent]
^
SyntaxError: 无效语法
或者 [word_len=(word, len(word)) result.append(word_len) for word in sent]
【问题讨论】:
-
赋值不是列表理解的一部分。您期望产生什么输出?
-
恐怕你的语法离太远了。也许你需要re-read the tutorial?