python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作
2023-09-14 09:01:12 时间
转自:http://blog.chinaunix.net/uid-200142-id-3992553.html
有时候,为了需求,需要统计两个 list 之间的交集,并集,差集。查询了一些资料,现在总结在下面:
1. 获取两个list 的交集
print list(set(a).intersection(set(b)))
2. 获取两个list 的并集
- print list(set(a).union(set(b)))
- 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':'20160909'} >>> bdict={'name':'lucy','id':1,'age':23,'birth':'20160909'} >>> adict {'age': 23, 'id': 1, 'birth': '20160909', 'name': 'lucy'} >>> bdict {'age': 23, 'name': 'lucy', 'birth': '20160909', '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', '20160909'), ('name', 'lucy')] >>> bdict.items() [('age', 23), ('name', 'lucy'), ('birth', '20160909'), ('id', 1)] >>> adict.update(bdict) >>> adict {'name': 'lucy', 'age': 23, 'birth': '20160909', 'id': 1} >>> abdict=adict.items()+bdict.items()#组合 >>> abdict [('name', 'lucy'), ('age', 23), ('birth', '20160909'), ('id', 1), ('age', 23), ('name', 'lucy'), ('birth', '20160909'), ('id', 1)] >>> abdict=dict(abdict) #转换为字典后去除了重复的键 >>> abdict {'age': 23, 'name': 'lucy', 'birth': '20160909', '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安装第三方库(离线+在线)
- Python获取时间戳_python爬虫时间戳
- Python怎么输入小数和整数_python输入非负整数
- python中矩阵的转置怎么写_Python 矩阵转置的几种方法小结
- python-列表(list)使用方法
- python教程:用简单的Python编写Web应用程序
- Python: list列表的内置方法介绍
- python chr()和ord()_Python函数ord
- python全局变量赋值_Python全局变量和局部变量[通俗易懂]
- python中的lambda表达式
- lambda表达式python_Python中的Lambda表达式「建议收藏」
- Python保存json_python保存json文件
- python win32api sendmessage_Python win32api.SendMessage方法代码示例[通俗易懂]
- python程序化交易实例-用 Python 实现你的量化交易策略「建议收藏」
- Python将数据写入txt文件_python将内容写入txt文件
- pycharm如何调试python程序_Pycharm断点调试Python程序的步骤方法
- 人生苦短,我用Python-手把手教你如何使用python写串口调试助手
- 微软会用Python替代VBA吗?
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(三)
- Python list列表添加元素的3种方法
- Hibernate Query接口 list方法:返回查询结果的List集合
- 快速查询MySQL数据库:Python篇(python查询mysql数据库)
- Redis 轻松存储列表数据:使用 Redis 保存 List 的方法(redis保存list)
- Python在连接MSSQL数据库中的应用(python连mssql)
- Linux下运行Python脚本的步骤简介(linux如何运行python脚本)
- python中List的sort方法指南