简体   繁体   English

Python Regex错误:“无法分配给运算符”

[英]Python Regex error: “Can't assign to operator”

I'm trying to work with Python and the NLTK but cannot seem to use regular expressions for them and cannot figure out why. 我正在尝试使用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

这里的问题是ed-words :在Python中-符号表示减号,因此它试图处理ed - words = ... ,这是非法的。

As others have mentioned, using - in a variable name is pretty much illegal in most programming languages. 正如其他人提到的那样,在大多数编程语言中,在变量名中使用-几乎是非法的。

In Python, you can use underscores: 在Python中,您可以使用下划线:

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

See Naming Conventions . 请参阅命名约定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM