python中对两个 list 求交集,并集和差集
python中对两个 list 求交集,并集和差集:
1、首先是较为浅白的做法:
>>> a=[1,2,3,4,5,6,7,8,9,10]
>>> b=[1,2,3,4,5]
>>> intersection=[v for v in a if v in b]
>>> intersection
[1, 2, 3, 4, 5]
>>> union=b.extend([v for v in a])
>>> union
>>> b
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> difference=[v for v in a if v not in b]
>>> difference
[] >>> a=[1,2,3,4,5,6,7,8,9,10]
>>> b=[1,2,3,4,5]
>>> difference=[v for v in a if v not in b]
>>> difference
[6, 7, 8, 9, 10]
2、效率较高的、并且可以去除重复数据的做法:
>>> a=[1,2,3,4,5,6,7,8,9,10]
>>> b=[1,2,3,4,5]
>>> intersection=list(set(a).intersection(set(b)))
>>> intersection
[1, 2, 3, 4, 5]
>>> union=list(set(a).union(set(b)))
>>> union
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> difference = list(set(a).difference(set(b)))
>>> difference
[8, 9, 10, 6, 7]
【Reference】
1、https://www.cnblogs.com/chen-kh/p/7137565.html
python中对两个 list 求交集,并集和差集的更多相关文章
- python中如何对list之间求交集,并集和差集
最近遇到一个从list a里面去除list b的元素的问题,由于a很大,b也不小.所以遇到点困难,现在mark一下. 先说最简单的方法: a = [1, 2, 3, 4, 5, 6, 7, 8, 9, ...
- python 两个list 求交集,并集,差集
def diff(listA,listB): #求交集的两种方式 retA = [i for i in listA if i in listB] retB = list(set(listA).inte ...
- Python中的两种结构dict和set
Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 假设要根据同学的名字查找对应的成绩 如果 ...
- python(leetcode)-350两个数组的交集
给定两个数组,编写一个函数来计算它们的交集. 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2,2] 示例 2: 输入: nums1 = [4,9,5 ...
- Linux 两个文件求交集、并集、差集
一.交集 sort a.txt b.txt | uniq -d 二.并集 sort a.txt b.txt | uniq 三.差集 a.txt-b.txt: sort a.txt b.txt b.tx ...
- python中保留两位小数
今天写程序的时候碰到了一个问题关于如何控制浮点数只显示小数点后两位,正常的想法是用round函数,例如 round(a, 2),但是在面对下面的问题时候round就不太好用了 >>> ...
- SQL Server中取两个表的交集,并集和差集
在项目中遇到要取两个表差集的情况 假设有两个表tblNZPostCodes, NZPostcode 两个表中存储的都是新西兰的post code信息,字段一致,只是数据上有所差异. 1. Union ...
- Python中的print、input函数以及Python中交换两个变量解析
一.Python中的值交换操作 首先明确一点点,Python中的一切都是面向对象的,可以理解为Python的中一切都是对象. 我们知道Java也是面向对象的语言,但是在Java中定义一个值变量如下: ...
- Python中的两种路径
Java中有两种路径,一种是操作系统的路径path,另一种是类路径classpath. Python中也是如此,一种是操作系统环境变量中的path,另一种是PYTHONPATH. 当import xx ...
随机推荐
- VS2013开发asmx接口根据ID查询对象
代码如下 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syst ...
- Log4net的不能产生Log文件的问题
[问题] 用如下的步骤应用了Log4Net: 建立了一个公用的项目, 在里面引入了Log4net的Nuget package. 在公用的项目中建立了一个类,加上了Log4net的attribute. ...
- Return array from functions in C++
C++ does not allow to return an entire array as an argument to a function. However, you can return a ...
- 设置Linux中的Mysql不区分表名大小写
1. MySQL数据库的表名在Linux系统下是严格区分大小写的,在Windows系统下开发的程序移植到Linux系统下,如果程序中SQL语句没有严格按照大小写访问数据库表,就可能会出现找不到表的错误 ...
- (C++)关于拷贝构造函数 Copy Constructor
题目: In which of the following scenarios is a Copy Constructor called or invoked? A. When no conve ...
- SVN 配置文件说明
svnserve是SVN自带的一个轻型服务器,客户端通过使用以svn://或svn+ssh://为前缀的URL来访问svnserve服务器,实现远程访问SVN版本库.svnserve可以通过配置文件来 ...
- Activity设置Dialog属性点击区域外消失实现方式
通过配置:<item name="android:windowCloseOnTouchOutside">true</item> 通过代码:setFinish ...
- 获取js连接参数js_args
获取js连接参数,如下以链接: <script src="js/jscript.js?skin=green" type="text/javascript" ...
- JS获取当前/指定URL参数
方法: 首先通过 document.location 获得当前访问网页的网址, 其次用 split 方法通过“?”把网址分为两部分. 如果网址中有参数(arrObj.length > 1) 再用 ...
- centos下安装配置mongodb
1:安装mkdir -p /app/mongodb tar zxvf mongodb-linux-x86_64-rhel62-3.4.6.tgz vi .bash_profile PATH=$PATH ...