0

我有一个简单的逻辑 if 语句返回无效的语法错误。声明是:

if (a[1] != None and a[2] != None and !(a[3] == None and a[4] == None)):

无效的语法是第三个!操作员。为什么这不起作用?在这种情况下我应该使用另一个运算符吗?

所以逻辑本质上是:(a[1] ^ a[2] ^ (a[3] v a[4])这些表示具有值)。None因此,没有值的反向逻辑是:

!a[1] ^ !a[2] ^ !(a[3] ^ a[4])

我很确定我的逻辑数学是正确的,那么我如何得到我需要的结果呢?

*背景信息:Python 2.7.10,整体代码是从 SQL Server 2008 表中提取数据,对其进行操作,然后将其插入到另一个不允许 NULL 值的表中,并且原始表中到处都是 NULL

谢谢你的帮助!

4

2 回答 2

6

python 中的逻辑非运算符 is not!而是 is not

你要:

if (a[1] != None and a[2] != None and not (a[3] == None and a[4] == None)):
于 2015-10-19T02:34:36.800 回答
2

好吧,从风格上讲,使用None时最好使用isandis not而不是==and !=,所以

if (a[1] is not None and a[2] is not None) and not (a[3] is None and a[4] is None):
于 2015-10-19T02:37:36.683 回答