python - 谁能解释为什么 True, True == (True, True) 的输出是 (True, False)?
问题描述
我尝试像下面的代码一样与 Python 进行比较,但对产生的输出感到困惑。
谁能解释为什么输出是这样的?
>>> True, True == True, True
(True, True, True)
>>> True, True == (True, True)
(True, False)
解决方案
因为运算符优先级。==
的优先级高于,
,因此第一个表达式被视为您已编写
True, (True == True), True
您的第二个表达式被视为
True, (True == (True, True))
如果要比较==
element-wise 的两侧,则需要在两侧加上括号:
(True, True) == (True, True)
这将返回True
。
请注意,逗号并不是严格意义上的 operator,因此为了理解这种行为,它已经足够接近了。
推荐阅读
- python - 如何从 unix 时间中获取正确的日期时间值?
- java - 使用多个命名空间解析 Youtube XML 响应
- ruby-on-rails - 仅当用户是当前用户的联系人时才显示用户的个人资料
- vue.js - 如何在 v-model 和 vue.js 中使用多维 json 中的动态键?
- excel - 将数组写入工作表并重复 n 次
- android-studio - 使用蓝牙耳机麦克风录音
- javascript - 获取 /index.html
- javascript - vuetify.js v-select 最小高度限制?
- javascript - 使用 ng-pattern 禁用 angularjs 中的按钮
- node.js - node.js 拒绝连接?