转自:http://blog.chinaunix.net/uid-200142-id-3992553.html

有时候,为了需求,需要统计两个 list 之间的交集,并集,差集。查询了一些资料,现在总结在下面:
1. 获取两个list 的交集
print list(set(a).intersection(set(b)))

2. 获取两个list 的并集
  1. print list(set(a).union(set(b)))

3. 获取两个 list 的差集

  1. print list(set(b).difference(set(a))) # b中有而a中没有的
>>> r=[1,2,3,4,5]
>>> m=[2,4]
>>> list(set(r).intersection(set(m)))
[2, 4]
>>> a=[1,2,3,4,5]
>>> b=[2,4,6,8]
>>> list(set(r).intersection(set(m)))
[2, 4]
>>> list(set(a).union(set(b)))
[1, 2, 3, 4, 5, 6, 8]
>>> list(set(b).difference(set(a))) # b中有而a中没有的
[8, 6]
>>> list(set(a).difference(set(b))) # a中有而b中没有的
[1, 3, 5]
>>> a=[3,4,2,5,1]
>>> list(set(r).intersection(set(m)))
[2, 4]
>>> list(set(a).union(set(b)))
[1, 2, 3, 4, 5, 6, 8]
>>> list(set(b).difference(set(a))) # b中有而a中没有的
[8, 6]
>>> list(set(a).difference(set(b))) # a中有而b中没有的
[1, 3, 5]
>>> #两个list比较的话,利用了set的属性,对各自元素的顺序无关,但是会过滤掉相同的元素

set操作:

>>> a=set(a)
>>> a
set([1, 2, 3, 4, 5])
>>> b=set(b)
>>> b
set([8, 2, 4, 6])
>>> a | b #求并集
set([1, 2, 3, 4, 5, 6, 8])
>>> a - b #求差集,a中有而b中无
set([1, 3, 5])
>>> b - a #求差集,b中有而a中没有的
set([8, 6])
>>> a & b #求交集
set([2, 4])
>>> a in b
False
>>> a not in b
True
>>> a==b
False
>>> a!=b
True
>>> len(a)
5
>>> a=set([1, 4, 3, 2, 5])
>>> a
set([1, 2, 3, 4, 5])
>>> a - b #求差集,a中有而b中无
set([1, 3, 5])
>>> #两个set比较,对各自元素的顺序无关,但是会过滤掉重复的元素
>>>

tuple操作:

>>> atuple
(1, 2, 3, 'd', 2, 3, 'n', 'd')
>>> btuple
(2, 3, 'n', 'd')
>>> cmp(btuple,btuple)
0
>>> tupleb=(2, 3, 'd', 'n')
>>> cmp(btuple,tupleb)
1
>>> cmp(tupleb,btuple)
-1
>>> #tuple比较与元素的顺序有关
>>> abtuple=atuple+btuple #组合
>>> abtuple
(1, 2, 3, 'd', 2, 3, 'n', 'd', 2, 3, 'n', 'd')
>>> aatuple=atuple*2 #复制
>>> aatuple
(1, 2, 3, 'd', 2, 3, 'n', 'd', 1, 2, 3, 'd', 2, 3, 'n', 'd')
>>> aatuple
(1, 2, 3, 'd', 2, 3, 'n', 'd', 1, 2, 3, 'd', 2, 3, 'n', 'd')
>>>

dict操作:

>>> adict={'id':1,'name':'lucy','age':23,'birth':''}
>>> bdict={'name':'lucy','id':1,'age':23,'birth':''}
>>> adict
{'age': 23, 'id': 1, 'birth': '', 'name': 'lucy'}
>>> bdict
{'age': 23, 'name': 'lucy', 'birth': '', 'id': 1}
>>> adict.keys()
['age', 'id', 'birth', 'name']
>>> bdict.keys()
['age', 'name', 'birth', 'id']
TypeError: unsupported operand type(s) for &: 'list' and 'list'
>>> adict.items()
[('age', 23), ('id', 1), ('birth', ''), ('name', 'lucy')]
>>> bdict.items()
[('age', 23), ('name', 'lucy'), ('birth', ''), ('id', 1)]
>>> adict.update(bdict)
>>> adict
{'name': 'lucy', 'age': 23, 'birth': '', 'id': 1}
>>> abdict=adict.items()+bdict.items()#组合
>>> abdict
[('name', 'lucy'), ('age', 23), ('birth', ''), ('id', 1), ('age', 23), ('name', 'lucy'), ('birth', ''), ('id', 1)]
>>> abdict=dict(abdict) #转换为字典后去除了重复的键
>>> abdict
{'age': 23, 'name': 'lucy', 'birth': '', 'id': 1}
>>> cdict={'name': 'kate', 'age': 23, 'birth': '20160909', 'id': 2}
>>> acdict=adict.items()+cdict.items()
>>> acdict
[('name', 'lucy'), ('age', 23), ('birth', '20160909'), ('id', 1), ('age', 23), ('name', 'kate'), ('birth', '20160909'), ('id', 2)]
>>> acdict=dict(acdict) #转换为字典后去除了重复的键,对于相同的键,选择最后合并进来的元素
>>> acdict
{'age': 23, 'name': 'kate', 'birth': '20160909', 'id': 2}
>>>

python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作的更多相关文章

  1. python实现基于两张图片生成圆角图标效果的方法

    python实现基于两张图片生成圆角图标效果的方法 这篇文章主要介绍了python实现基于两张图片生成圆角图标效果的方法,实例分析了Python使用pil模块进行图片处理的技巧,分享给大家供大家参考. ...

  2. 转-oracle中比较两表表结构差异和数据差异的方法

    oracle中比较两表表结构差异和数据差异的方法 原作者:li2008xue2008ling  出处:http://blog.csdn.net       在工作中需要完成这么一个需求:比较两个表的表 ...

  3. Python学习笔记:set集合类型所有方法汇总

    ################################################## 集合的作用是:# 1.获得两个集合之间某种关系的集合(比如求两个集合的交集)# 2.计算集合之间的 ...

  4. jQuery 选择同时包含两个class的元素的实现方法

    Jquery选择器 多个 class属性参照以下案例 <element class="a b good list card"> 1. 交集选择: $(".a. ...

  5. Python中执行系统命令常见的几种方法--转载

    Python中执行系统命令常见的几种方法 Python中执行系统命令常见的几种方法有: (1)os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 # 如果再命令行下执 ...

  6. python之列表(list)的使用方法介绍

    python之列表(list)介绍 在python的使用过程中,我们经常会用到列表,然而经常会遇到疑惑,下面我将详细介绍下列表使用方法. 一.列表 列表经常用到的功能使增.删.改和查功能. 1. 增 ...

  7. Python 内编写类的各种技巧和方法

    Python 内编写类的各种技巧和方法 简介 有关 Python 内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象 ...

  8. Python中使用多进程来实现并行处理的方法小结

    进程和线程是计算机软件领域里很重要的概念,进程和线程有区别,也有着密切的联系,先来辨析一下这两个概念: 1.定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和 ...

  9. Python中日期和时间格式化输出的方法

    本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...

  10. Python判断列表是否已排序的各种方法及其性能分析

    目录 Python判断列表是否已排序的各种方法及其性能分析 声明 一. 问题提出 二. 代码实现 2.1 guess 2.2 sorted 2.3 for-loop 2.4 all 2.5 numpy ...

随机推荐

  1. https://my.oschina.net/reesechou/blog/492265

    https://my.oschina.net/reesechou/blog/492265

  2. iOS: 讯飞语音的使用

    一.介绍: 讯飞语音做的相当不错,容错率达到90%多,如果需要做语音方面的功能,它绝对是一个不错的选择.讯飞语音的功能很多:语音听写.语音识别.语音合成等,但我们最常用的还是语音听写.讯飞语音中包含界 ...

  3. Linux中硬件相关命令

    http://www.cnblogs.com/cchust/p/3354570.html http://www.cnblogs.com/kerrycode/archive/2012/07/06/257 ...

  4. [PCL]1 PCL点云库安装

    1.安装文件下载:官网,我还是比较喜欢别人编译好的安装包啊,哈哈. http://www.pointclouds.org/downloads/windows.html 2.傻瓜式安装(下面的依赖项都集 ...

  5. 浅谈My SQL引擎的对比

    MySQL数 据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译MYSQL.在缺省情况下,MYSQL支持三个引擎:ISAM.MYISAM和HEAP.另外两种类型IN ...

  6. FtpWebRequest FTP异步下载、异步上传文件

    异步下载: public interface IPrimaryKey<T> { T GetKey(); } public class DownloadInfo : IPrimaryKey& ...

  7. 查询功能:yum [list|info|search|provides|whatprovides] 参数

    [root@www ~]# yum [option] [查询工作项目] [相关参数] 选项与参数: [option]:主要的选项,包括有:   -y :当 yum 要等待使用者输入时,这个选项可以自动 ...

  8. java-语法

    JAVA语法 1.标识符 1.定义:对各种变量.方法.类等进行命名的字符序列 2.规则:他的组成由字母.数字.$,数字不能出现在开始,不能和关键字重复,区分大小写 2.数据类型 1.分类 1基本数据类 ...

  9. .NET: 防止多个应用程序同时开

    用到了Mutex这个类,直接看代码~ using System; using System.Collections.Generic; using System.Linq; using System.W ...

  10. poj: 1207

    好吧这题竟然还有先大后小的可能,能不这么恶心下吗.. #include <iostream> #include <stdio.h> #include <string.h& ...