python两个 list 交集,并集,差集的方法+两个tuple比较操作+两个set的交集,并集,差集操作+两个dict的比较操作

时间:2023-12-04 14:53:08

转自: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}
>>>