python获得两个数组交集、并集、差集的方法
作者:songguo 时间:2022-08-12 18:37:31
本文实例讲述了python获得两个数组交集、并集、差集的房部分。分享给大家供大家参考。具体如下:
1. 获取两个list 的交集
#方法一:
a=[2,3,4,5]
b=[2,5,8]
tmp = [val for val in a if val in b]
print tmp
#[2, 5]
#方法二
print list(set(a).intersection(set(b)))
2. 获取两个list 的并集
print list(set(a).union(set(b)))
3. 获取两个 list 的差集
print list(set(b).difference(set(a))) # b中有而a中没有的
通过以上方法,就能处理python list 的交集,并集,差集了。
希望本文所述对大家的Python程序设计有所帮助。
标签:python,数组,交集,并集,差集
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python defaultdict方法使用分析
2023-01-02 18:05:35
![](https://img.aspxhome.com/file/2023/2/83682_0s.png)
全面了解python中的类,对象,方法,属性
2021-10-07 10:54:50
Python2与Python3的区别实例总结
2021-05-20 00:04:24
Python的 元组(Tuple)详解
2021-09-07 13:55:18
![](https://img.aspxhome.com/file/2023/4/120334_0s.png)
基于Python实现的购物商城管理系统
2021-08-27 15:06:43
Python 读取 .gz 文件全过程
2021-11-02 16:01:04
python接口自动化测试数据和代码分离解析
2022-12-29 06:00:11
![](https://img.aspxhome.com/file/2023/9/97669_0s.png)
Python基于scrapy采集数据时使用代理服务器的方法
2022-06-03 08:37:04
Python用 matplotlib 绘制柱状图
2023-01-22 01:20:50
![](https://img.aspxhome.com/file/2023/6/123796_0s.png)
关于python下cv.waitKey无响应的原因及解决方法
2022-03-04 14:51:21
Python3 使用pillow库生成随机验证码
2021-08-30 02:54:10
mysql 各种时间段查询
2010-01-06 13:37:00
python之json文件转xml文件案例讲解
2021-11-18 04:54:23
![](https://img.aspxhome.com/file/2023/8/107668_0s.jpg)
python实现简单贪吃蛇小游戏
2021-12-11 13:59:00
Python实现发送与接收邮件的方法详解
2023-04-05 04:48:43
![](https://img.aspxhome.com/file/2023/7/107847_0s.png)
如何实现让每句话的头一个字母都大写?
2010-05-24 18:26:00
python识别围棋定位棋盘位置
2023-01-09 01:49:34
![](https://img.aspxhome.com/file/2023/9/90799_0s.jpg)
Python实现端口复用实例代码
2023-12-16 19:58:32
Python3爬虫RedisDump的安装步骤
2023-07-17 00:42:15
Scrapy爬虫框架集成selenium及全面详细讲解
2021-07-28 18:47:09
![](https://img.aspxhome.com/file/2023/2/95832_0s.jpg)