python比较运算符连用问题(双等号连用)
来源:互联网 发布:win7所有软件都打不开 编辑:程序博客网 时间:2024/05/01 21:01
在python神奇的世界里
你可能见过这个样的语句:
if (5 == len(set(vec[i]+i for i in cols))
== len(set(vec[i]-i for i incols))):
print vec
或是这样的语句:
a=[1,2,3,4,5]
b=[1,2,3,4,5]
if(5 ==len(a)
==len(b)):
print "yes"
else:
print "no"
你可能会疑惑“==”比较符连在一起是什么意思?
按照c的语法规则,显然我们要先从左边第一个“==”开始,
以上题为例,若“5 ==len(a)”则返回布尔值“True”,然后再比较“True”和“len(b)”是否相等。但是根据python例程的使用情况,我们推测出,python中的“5==len(a)== len(b)”显然不是这样比较的。
我们在IDLE中输入“help('==')”,得到以下信息,我只摘录出主要部分,其他的信息读者有兴趣的话可以自己输入命令查看。
Unlike C, all comparison operations in Python have the same priority,which is lower than that of any arithmetic, shifting or bitwiseoperation.……
Comparisons can be chained arbitrarily, e.g.,
"x < y <= z" isequivalent to "x < y and y <= z",……
按照该官方文档解释,
”5== len(a)== len(b)“等价于”5== len(a) and len(a)==len(b)“
这样是不是就一目了然了呢?
- python比较运算符连用问题(双等号连用)
- C++ *& 连用
- 关于cin,和cin.getline()连用问题
- 解决cin.getline()函数被跳过执行的问题(与cin连用)
- *& 连用,用于返回类型
- ComboBox和vector连用
- typedef 与 指针连用
- typedef与指针连用
- typedef 与 指针连用
- typedef指针连用
- 三个滚动视图连用
- TabLayout和Viewpage连用
- viewpager,fragment连用
- HorizontalScrollView与 radioButton 连用
- try-finally连用
- if condition连用
- TabLayout+PullToRefreshListView连用
- TabLayout+PullToRefreshListView连用
- textarea标签和label标签让我郁闷的时候
- 【Java】【Exception】Java Error And Exception
- height、clientHeight、scrollHeight、offsetHeight区别
- 3D - AVS 里面的一些变量缩写
- Heritrix eclipse创建java可运行程序的步骤
- python比较运算符连用问题(双等号连用)
- python -- pywin32安装
- Java回调函数
- Codeforces 320B Ping-Pong (Easy Version)
- 查看表空间碎片量
- Spark1.0.0 运行架构基本概念
- 代码片段
- php的安装在centos上
- GDB常用命令