python如何比较两个list是否相同

Python2可以使用cmp()函数来比较两个list是否相等。

复制代码
a=[1,-1,0]
b=[1,-1,0]
c=[-1,1,0]
print cmp(a, b)
print cmp(a, c)

结果输出

复制代码
0
1

cmp(list1 ,list2) ,

当list1<list2会返回负数 -1、

当list1>list2会返回正数 1、

当list1=list2则返回0。

list1=list2一定是两个列表必须完全相同(包括位置),只有这样才能是0。

但是在Python3中我们可以使用operator方法来比较两个list是否相等。

复制代码
import operator
 
a=[1,-1,0]
b=[1,-1,0]
c=[-1,1,0]
print(operator.eq(a,b))
print(operator.eq(a,c))

实验结果:

复制代码
D:\pycharmprogram\leetcode\venv\Scripts\python.exe D:/pycharmprogram/leetcode/3Sum/operator_test.py
True
False
 
Process finished with exit code 0

分析:

两个列表必须完全相同(包括位置),只有这样才能是True。

相关推荐
gmaajt10 分钟前
mysql如何检查数据库表是否存在损坏_使用CHECK TABLE命令修复
jvm·数据库·python
heRs BART23 分钟前
【Flask】四、flask连接并操作数据库
数据库·python·flask
PyHaVolask1 小时前
Python 爬虫进阶:直接请求 JSON 接口与开发者工具使用
爬虫·python·请求头·反爬·json接口·chrome开发者工具
larance1 小时前
安装dify的几个问题
python
2301_773553621 小时前
CSS如何对用户访问过的链接进行降级颜色处理_使用-visited伪类改变颜色
jvm·数据库·python
2301_815279521 小时前
Golang怎么理解Go的sync.Pool底层_Golang如何理解Pool的本地缓存和GC清理机制【详解】
jvm·数据库·python
2301_764150561 小时前
MySQL迁移过程如何避免数据不一致_利用强一致性备份方案
jvm·数据库·python
m0_716430071 小时前
Redis如何处理预热失效引起的开局雪崩
jvm·数据库·python
m0_377618231 小时前
c++文件锁使用方法 c++如何实现多进程文件同步
jvm·数据库·python
gmaajt1 小时前
mysql多字段搜索如何设计组合索引_mysql索引查询加速
jvm·数据库·python