python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作
转自:http://blog.chinaunix.net/uid-200142-id-3992553.html
有时候,为了需求,需要统计两个 list 之间的交集,并集,差集。查询了一些资料,现在总结在下面:
1. 获取两个list 的交集
print list(set(a).intersection(set(b)))
- print list(set(a).union(set(b)))
3. 获取两个 list 的差集
- 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的比较操作的更多相关文章
- python实现基于两张图片生成圆角图标效果的方法
python实现基于两张图片生成圆角图标效果的方法 这篇文章主要介绍了python实现基于两张图片生成圆角图标效果的方法,实例分析了Python使用pil模块进行图片处理的技巧,分享给大家供大家参考. ...
- 转-oracle中比较两表表结构差异和数据差异的方法
oracle中比较两表表结构差异和数据差异的方法 原作者:li2008xue2008ling 出处:http://blog.csdn.net 在工作中需要完成这么一个需求:比较两个表的表 ...
- Python学习笔记:set集合类型所有方法汇总
################################################## 集合的作用是:# 1.获得两个集合之间某种关系的集合(比如求两个集合的交集)# 2.计算集合之间的 ...
- jQuery 选择同时包含两个class的元素的实现方法
Jquery选择器 多个 class属性参照以下案例 <element class="a b good list card"> 1. 交集选择: $(".a. ...
- Python中执行系统命令常见的几种方法--转载
Python中执行系统命令常见的几种方法 Python中执行系统命令常见的几种方法有: (1)os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 # 如果再命令行下执 ...
- python之列表(list)的使用方法介绍
python之列表(list)介绍 在python的使用过程中,我们经常会用到列表,然而经常会遇到疑惑,下面我将详细介绍下列表使用方法. 一.列表 列表经常用到的功能使增.删.改和查功能. 1. 增 ...
- Python 内编写类的各种技巧和方法
Python 内编写类的各种技巧和方法 简介 有关 Python 内编写类的各种技巧和方法(构建和初始化.重载操作符.类描述.属性访问控制.自定义序列.反射机制.可调用对象.上下文管理.构建描述符对象 ...
- Python中使用多进程来实现并行处理的方法小结
进程和线程是计算机软件领域里很重要的概念,进程和线程有区别,也有着密切的联系,先来辨析一下这两个概念: 1.定义 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和 ...
- Python中日期和时间格式化输出的方法
本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...
- Python判断列表是否已排序的各种方法及其性能分析
目录 Python判断列表是否已排序的各种方法及其性能分析 声明 一. 问题提出 二. 代码实现 2.1 guess 2.2 sorted 2.3 for-loop 2.4 all 2.5 numpy ...
随机推荐
- Python开发【第二章】:Python的数据类型
基本数据类型 一.整型 如: 18.73.84 整型具备如下功能: class int(object): """ int(x=0) -> int or long i ...
- 安装 zsh 、 on-my-zsh 和 autojump
安装 zsh . on-my-zsh 和 autojump zsh 是 linux 上另外一个 shell ,号称是终极 shell .它的配置比较复杂,一般的发行版中,默认没有安装这个 shell ...
- JQuery 操作HTML元素 示例
http://www.w3school.com.cn/jquery/jquery_dom_add.asp http://www.w3school.com.cn/jquery/jquery_dom_re ...
- jQuery框架的简单使用(H5)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 帮你深入理解OAuth2.0协议
1. 引言 如果你开车去酒店赴宴,你经常会苦于找不到停车位而耽误很多时间.是否有好办法可以避免这个问题呢?有的,听说有一些豪车的车主就不担心这个问题.豪车一般配备两种钥匙:主钥匙和泊车钥匙.当你到酒店 ...
- mysql explain 命令讲解
explian命令可以显示select语句的执行计划 explain的结果中每行对应select语句中的一个表,输出结果中的顺序是按照语句处理表的顺序. mysql使用嵌套循环来处理所有的join连接 ...
- Java基础之一组有用的类——使用比较器对数组排序(TrySortingWithComparator)
控制台程序. Arrays类中的sort()静态方法把传送为参数的数组元素按升序方式排序. 对于第一个参数类型是Object[]的sort()方法来说,可以传送任意类型的数组.如果使用sort()方法 ...
- PAT 解题报告 1050. String Subtraction (20)
1050. String Subtraction (20) Given two strings S1 and S2, S = S1 - S2 is defined to be the remainin ...
- The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation. 怎么解决
cd 文件 pod install --no-repo-update 就可以了
- PostgreSQL Replication之第十二章 与Postgres-XC一起工作(1)
在本章中,我们希望将我们的注意力集中在写可扩展,多主,同步,对称和PostgreSQL的称为Postgres-XC(PostgreSQL eXtensible Cluster)的透明复制方案.该项目的 ...