在python3.7.1对列表的处理中,会经常使用到Python求两个list的差集、交集与并集的方法。

下面就以实例形式对此加以分析。

# 求两个list的差集、并集与交集
# 一.两个list差集
#
# 如有下面两个数组: a = [1, 2, 3] b = [2, 3]
# 想要的结果是[1]
#
# 下面记录一下三种实现方式:
#
# 1. 正常的方式 # ret = []
# for i in a:
# if i not in b:
# ret.append(i)
#
# print(ret)
# 2.简化版 # ret = [i for i in a if i not in b]
# print(ret)
# 3.高级版 # ret = list(set(a) ^ set(b))
# print(ret)
# 4.最终版
# result = list(set(a).difference(set(b))) # a中有而b中没有的 不建议使用
# result2 = list(set(b).difference(set(a))) # b中有而a中没有的 不建议使用
# print(result) 输出结果为[1]
# print(result2) 输出结果为[]

# 二.两个list并集
result = list(set(a).union(set(b)))
print(result) # 三.两个list交集
result = list(set(a).intersection(set(b)))
print(result)

Python3.7.1学习(三)求两个list的差集、并集与交集的更多相关文章

  1. CDOJ 1104 求两个数列的子列的交集 查询区间小于A的数有多少个 主席树

    求两个数列的子列的交集 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1104 ...

  2. Python 求两个文本文件以行为单位的交集 并集 差集

    Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...

  3. 【转载】 C#使用Union方法求两个List集合的并集数据

    在C#语言的编程开发中,有时候需要对List集合数据进行运算,如对两个List集合进行交集运算或者并集运算,其中针对2个List集合的并集运算,可以使用Union方法来快速实现,Union方法的调用格 ...

  4. 用shell求两个文件的差集

    假设有两个文件a.file和b.file,分别代表集合A和集合B. a.file的内容如下: abcde b.file的内容如下: cdefg 可以用grep命令 grep命令是常用来搜索文本内容的, ...

  5. php 求两个数组的差集应该注意的事情

    对于 phper 来说 array_diff 这个函数应该知道它的用途,获取两个数组的差集,我理解中的差集是这样的 但是执行下代码会发现结果并不是 <?php $a = [1,2,3,4,5]; ...

  6. python3.5.3rc1学习三:文件操作

    ##全局变量与局部变量x = 6 def printFuc(): y = 8 z =9 print(y + z) print(x) printFuc()#print(y)#常见错误##name = & ...

  7. java求两个集合的差集

    public static void main(String[] args) {Set set = new HashSet();Set set1 = new HashSet();set.add(&qu ...

  8. arcgis如何求两个栅格数据集的差集

    栅格数据集没有擦除功能,现在有栅格A和栅格B,怎么求两个栅格的差集C 具体步骤如下: 1.首先利用栅格计算器,把栅格B中的value全部赋值为0 输入语句:"栅格B" * 0 2  ...

  9. python求两个列表的并集.交集.差集

    求两个列表的差集 >>> a = [1,2,3] >>> b=[1,2] >>> ################################ ...

随机推荐

  1. The Mininum Function Value (luoguP2085 最小函数值)

    Background\text{Background}Background 1. CSDN's been upgraded today. There's one MORE ad for each pa ...

  2. Spring Boot项目中如何定制PropertyEditors

    本文首发于个人网站:Spring Boot项目中如何定制PropertyEditors 在Spring Boot: 定制HTTP消息转换器一文中我们学习了如何配置消息转换器用于HTTP请求和响应数据, ...

  3. Etcd安装和使用

    Etcd安装和使用 一.安装 1.1 二进制安装 从这里下载: etcd-v3.2.11-linux-amd64.tar.gz 下载包后解压即可运行: # 解压 tar zxvf etcd-v3.2. ...

  4. lable的渲染

    <StackPanel Margin=" TextOptions.TextFormattingMode="Display"> <Label TextOp ...

  5. 数组转换成List集合

    Element[] array = {new Element(1), new Element(2)};List<element> list = new ArrayList<eleme ...

  6. int和string的相互装换 (c++)

    int和string的相互装换 (c++) int转换为string 第一种方法 to_string函数,这是c++11新增的函数 string to_string (int val); string ...

  7. 快速搭建spring boot2.0 项目

    快速搭建spring boot2.0+mybatis+thymeleaf 项目 使用工具STS 3.9.7(eclipse) 首先创建一个spring boot2.0项目(具体创建方法就不写了) 然后 ...

  8. URL中文参数,JSON转换,PHP赋值JS

    var jsonProps = { "dispMode":dispMode, "autoRun":autoRun, "clientPath" ...

  9. python实现输入任意一个大写字母生成金字塔的示例

    输入任意一个大写字母,生成金字塔图形 def GoldTa(input): L = [chr(i) for i in range(65, 91)] # 大写字母A--Z idA = 65 # 从A开始 ...

  10. Spring Cloud alibaba网关 sentinel zuul 四 限流熔断

    spring cloud alibaba 集成了 他内部开源的 Sentinel 熔断限流框架 Sentinel 介绍 官方网址 随着微服务的流行,服务和服务之间的稳定性变得越来越重要.Sentine ...