Python正则表达式错误: "Can' t assign to operator"

标签 python python-2.7

我正在尝试使用 Python 和 NLTK,但似乎无法对它们使用正则表达式,并且无法找出原因。

>>> import nltk
>>> import re
>>> words = nltk.corpus.words.words('en')
>>> ed-words = [w for w in words if re.search('ed$', w)]
SyntaxError: can't assign to operator

最佳答案

正如其他人提到的,在变量名中使用 - 在大多数编程语言中几乎是非法的。

在Python中,您可以使用下划线:

ed_words = [w for w in words if re.search('ed$', w)]

参见Naming Conventions .

关于Python正则表达式错误: "Can' t assign to operator",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19752632/

相关文章:

python - 选择网格的所有顶点以分别访问每个顶点

python - 如何使用 MySQL Python Connector 确保 MySQL 连接返回到连接池?

python - 谷歌 foobar 挑战错误

python - 在 BOINC 上运行 Python 应用程序

python - 如何在1到100的数字范围内查找包含特定数字的所有数字?

python - 我们可以只计算一个非常大的稀疏矩阵的第 n 个特征值和特征向量吗?

python - 使用flask找不到flask.ext.pagedown模块

python - 将列表作为多个参数传递

python - 理解 decode() 和 encode() unicode

python - 尝试读取 yaml 文件时出现错误 "found unhashable key"