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