投放广告联系QQ:82522688

Python3列表(list)比较操作教程

admin 量化学习 2023-03-27 17:38:29 python   list

Python3列表(list)比较操作教程

一、相等比较

1.1 同顺序列表比较

顺序相同直接用“==”进行比较即可

list1 = ["one","two","three"]
list2 = ["one","two","three"]
list1 == list2
>>>list1 ["one","two","three"]
>>>list2 ["one","two","three"]
>>>list1 ==list2
True
 

2.1 不同顺序列表进行比较

“==”只有成员、成员位置都相同时才返回True,但有时候我们希望只要成员相同、即使成员位置不同也能返回True。

>>>list1 ["one","two","three"]
>>>list2 ["one","three","two"]
>>>list1 =list2
False

2.1.1 使用列表sort()方法进行排序后比较

列表本身有sort()内置方法,可对自身成员进行排序;注意sort()方法对自身造成改变。

list1 = ["one","two","three"]
list2 = ["one","three","two"]
list1.sort() == list2.sort()
print(list1)
list1 ["one","two","three"]

>>>list2 ["one","three","two"]
>>>list1.sort()==list2.sort()
True
>>>print(list1)
['one','three','two']
 

2.1.2 使用sorted()方法进行排序后比较

上一小节介绍的sort()方法会对列表成员进行重排,但有时候我们并不希望列表本身被改动。

我们可以用一下变量将原先的列表保存起来,但更好的做法是使用sorted()方法,sorted()不改变列表原本顺序而是新生成一个排序后的列表并返回。

list1 = ["one","two","three"]
list2 = ["one","three","two"]
sorted(list1) == sorted(list2)
print(list1)
sorted(list1)
>>>list1 ["one","two","three"]
>>>list2 ["one","three","two"]
>>>sorted(list1)==sorted(list2)
True

>>>print (list1)
['one',two',three'

>>>sorted(list1)
['one','three',two'
 

二、包含比较

直接用列表本身进行包含类比较,只能用遍历的方法这是比较麻烦的,使用set()转成集合进行包含比较就简单多了。

 

2.1 判断列表是否包含另一列表

list1 = ["one","two","three"]
list2 = ["one","three","two","four"]
set(list1).issubset(set(list2))
set(list2).issuperset(set(list1))
>>>list1["one","two","three"]
>>>list2 ["one","three","two","four"]
>>>set(list1).issubset(set(list2))
True
>>>set (list2).issuperset (set (list1))
True
 

2.2 获取两个列表相同成员(交集)

list1 = ["one","two","three","five"]
list2 = ["one","three","two","four"]
set(list1).intersection(set(list2))
>>>list1 ["one","two","three","five"]
>>>list2 ["one","three","two","four"]
>>>set (list1).intersection(set(list2))
{two',three',one'}
 

2.3 获取两个列表不同成员

list1 = ["one","two","three","five"]
list2 = ["one","three","two","four"]
set(list1).symmetric_difference(set(list2))

>>>list1 ["one","two","three","five"]
>>>list2 ["one","three","two","four"]
>>>set(list1).symmetric difference (set (list2))
{'five','four'}

2.4 获取一个列表中不是另一个列表成员的成员(差集)

list1 = ["one","two","three","five"]
list2 = ["one","three","two","four"]
set(list1).difference(set(list2))
set(list2).difference(set(list1))
>>>list1 ["one","two","three","five"]
>>>list2 ["one","three","two","four"]
>>>set(list1).difference(set (list2))
{'five'}
>>>set(list2).difference(set(list1))
{'four'}
 

2.5 获取两个列表所有成员(并集)

list1 = ["one","two","three","five"]
list2 = ["one","three","two","four"]
set(list1).union(set(list2))
>>>list1 ["one","two","three","five"]
>>>list2 ["one","three","two","four"]
>>>set(list1).union(set (list2))
{'five','one','two','three','four')
郑重声明 本文只是个人(本单位)复盘记录,文内提到的所有信息仅为分享和盘面结构梳理,不构成投资或投机建议,买卖自行决策,结果自己负责。
 

阅读与下载说明

1.会员阅览(扣点)为普通会员扣点(1元=1点)通道,已浏览过的只扣一次。
2.VIP阅览 (VIP) 为VIP特权通道,充值成VIP用户直接无任何限制高速在线阅览,VIP会员分包月,包年和终身VIP三种。
3.免费阅览(免费)为未付费会员通道,可无任何限制免费阅览该资源,推荐购买点数充值VIP,以获取超值资源。
4.  阅览的资讯文件过大,根据您的网速而会有相应的延迟,请耐心等待;如果提示其他问题请联系客户解决。
 

温馨提示:
购买点数充值VIP全部支持支付宝或微信扫码支付,登陆会员中心侧面板>>财务选择相关操作即可。
在线预览主旨方便移动设备使用和临时查看,直观浏览,对需要的文章再下载,预览了的文件且能秒速下载。

分享:

扫一扫在手机阅读、分享本文