Python必備技巧之集合Set的使用
在數(shù)學(xué)中,對(duì)集合的嚴(yán)格定義可能是抽象的且難以掌握。但實(shí)際上可以將集合簡(jiǎn)單地認(rèn)為是定義明確的不同對(duì)象的集合,通常稱為元素或成員。
Python 提供了一個(gè)內(nèi)置的集合類型來(lái)將對(duì)象分組到一個(gè)集合中。集合與其他對(duì)象類型的區(qū)別在于可以對(duì)執(zhí)行的獨(dú)特操作。
定義一個(gè)集合
集合是無(wú)序的,并且元素是唯一的,集合本身可以修改,但集合中包含的元素必須是不可變類型。
構(gòu)建集合的方式
# 構(gòu)建的set數(shù)據(jù)會(huì)自動(dòng)進(jìn)行去重 x = set(<iter>) # list方式 >>> x = set(['foo', 'bar', 'baz', 'foo', 'qux']) >>> x {'qux', 'foo', 'bar', 'baz'} # tuple方式 >>> x = set(('foo', 'bar', 'baz', 'foo', 'qux')) >>> x {'qux', 'foo', 'bar', 'baz'} # 字符串方式 >>> s = 'quux' >>> list(s) ['q', 'u', 'u', 'x'] >>> set(s) {'x', 'u', 'q'}
集合元素set后自動(dòng)排序并且元素必須是不可變的。
>>> x = {42, 'foo', (1, 2, 3), 3.14159} >>> x {42, 'foo', 3.14159, (1, 2, 3)} # list和dict不能被set >>> a = [1, 2, 3] >>> {a} Traceback (most recent call last): File "<pyshell#70>", line 1, in <module> {a} TypeError: unhashable type: 'list' >>> d = {'a': 1, 'b': 2} >>> vvxyksv9kd Traceback (most recent call last): File "<pyshell#72>", line 1, in <module> vvxyksv9kd TypeError: unhashable type: 'dict'
集合的大小和成員資格
方法 len() 、in 、 not in 的應(yīng)用。
>>> x = {'foo', 'bar', 'baz'} >>> len(x) 3 >>> 'bar' in x True >>> 'qux' in x False
集合的9種操作
計(jì)算集合并集
# x1.union(x2[, x3 ...]) # x1 | x2 [| x3 ...] >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'baz', 'qux', 'quux'} >>> x1 | x2 {'baz', 'quux', 'qux', 'bar', 'foo'} >>> x1.union(x2) {'baz', 'quux', 'qux', 'bar', 'foo'} # 更多的集合并集操作 >>> a = {1, 2, 3, 4} >>> b = {2, 3, 4, 5} >>> c = {3, 4, 5, 6} >>> d = {4, 5, 6, 7} >>> a.union(b, c, d) {1, 2, 3, 4, 5, 6, 7} >>> a | b | c | d {1, 2, 3, 4, 5, 6, 7}
計(jì)算集合交集
# x1.intersection(x2[, x3 ...]) # x1 & x2 [& x3 ...] >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'baz', 'qux', 'quux'} >>> x1.intersection(x2) {'baz'} >>> x1 & x2 {'baz'} # 更多的集合交集操作 >>> a = {1, 2, 3, 4} >>> b = {2, 3, 4, 5} >>> c = {3, 4, 5, 6} >>> d = {4, 5, 6, 7} >>> a.intersection(b, c, d) {4} >>> a & b & c & d {4}
計(jì)算集合之間差異
# x1.difference(x2[, x3 ...]) # x1 - x2 [- x3 ...] >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'baz', 'qux', 'quux'} >>> x1.difference(x2) {'foo', 'bar'} >>> x1 - x2 {'foo', 'bar'} # 更多的集合差異操作 >>> a = {1, 2, 3, 30, 300} >>> b = {10, 20, 30, 40} >>> c = {100, 200, 300, 400} >>> a.difference(b, c) {1, 2, 3} >>> a - b - c {1, 2, 3}
計(jì)算集合間對(duì)稱差
# x1.symmetric_difference(x2) # x1 ^ x2 [^ x3 ...] >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'baz', 'qux', 'quux'} >>> x1.symmetric_difference(x2) {'foo', 'qux', 'quux', 'bar'} >>> x1 ^ x2 {'foo', 'qux', 'quux', 'bar'} # 更多的集合對(duì)稱差操作 >>> a = {1, 2, 3, 4, 5} >>> b = {10, 2, 3, 4, 50} >>> c = {1, 50, 100} >>> a ^ b ^ c {100, 5, 10}
計(jì)算后集合中是否有包含前集合的元素
# x1.isdisjoint(x2) >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'baz', 'qux', 'quux'} >>> x1.isdisjoint(x2) False >>> x2 - {'baz'} {'quux', 'qux'} >>> x1.isdisjoint(x2 - {'baz'}) True # x1.isdisjoint(x2)是True,那么x1 & x2是空集 >>> x1 = {1, 3, 5} >>> x2 = {2, 4, 6} >>> x1.isdisjoint(x2) True >>> x1 & x2 set()
計(jì)算一個(gè)集合是否是另一個(gè)集合的子集
# x1.issubset(x2) # x1 <= x2 >>> x1 = {'foo', 'bar', 'baz'} >>> x1.issubset({'foo', 'bar', 'baz', 'qux', 'quux'}) True >>> x2 = {'baz', 'qux', 'quux'} >>> x1 <= x2 False # 一個(gè)集合被認(rèn)為是它自身的一個(gè)子集 >>> x = {1, 2, 3, 4, 5} >>> x.issubset(x) True >>> x <= x True
計(jì)算一個(gè)集合是否是另一個(gè)集合的真子集
# x1 < x2 >>> x1 = {'foo', 'bar'} >>> x2 = {'foo', 'bar', 'baz'} >>> x1 < x2 True >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'foo', 'bar', 'baz'} >>> x1 < x2 False # 子集與真子集的判斷 >>> x = {1, 2, 3, 4, 5} >>> x <= x True >>> x < x False
計(jì)算一個(gè)集合是否是另一個(gè)集合的超集
# x1.issuperset(x2) # x1 >= x2 >>> x1 = {'foo', 'bar', 'baz'} >>> x1.issuperset({'foo', 'bar'}) True >>> x2 = {'baz', 'qux', 'quux'} >>> x1 >= x2 False # 集合被認(rèn)為是本身的一個(gè)子集,默認(rèn)為自身超集 >>> x = {1, 2, 3, 4, 5} >>> x.issuperset(x) True >>> x >= x True
計(jì)算一個(gè)集合是否是另一個(gè)集合的正確超集
# x1 > x2 >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'foo', 'bar'} >>> x1 > x2 True >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'foo', 'bar', 'baz'} >>> x1 > x2 False # 集合不是其自身的正確超集 >>> x = {1, 2, 3, 4, 5} >>> x > x False
集合的9種修改
盡管集合中包含的元素必須是不可變類型,但集合本身可以修改。
update計(jì)算并集
# x1.update(x2[, x3 ...]) # x1 |= x2 [| x3 ...] >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'foo', 'baz', 'qux'} >>> x1 |= x2 >>> x1 {'qux', 'foo', 'bar', 'baz'} >>> x1.update(['corge', 'garply']) >>> x1 {'qux', 'corge', 'garply', 'foo', 'bar', 'baz'}
intersection_update 計(jì)算交集
# x1.intersection_update(x2[, x3 ...]) # x1 &= x2 [& x3 ...] >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'foo', 'baz', 'qux'} >>> x1 &= x2 >>> x1 {'foo', 'baz'} >>> x1.intersection_update(['baz', 'qux']) >>> x1 {'baz'}
difference_update 按差異修改被處理集合
>>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'foo', 'baz', 'qux'} >>> x1 -= x2 >>> x1 {'bar'} >>> x1.difference_update(['foo', 'bar', 'qux']) >>> x1 set()
symmetric_difference_update 按對(duì)稱差修改被處理集合
# x1.symmetric_difference_update(x2) # x1 ^= x2 >>> x1 = {'foo', 'bar', 'baz'} >>> x2 = {'foo', 'baz', 'qux'} >>> x1 ^= x2 >>> x1 {'bar', 'qux'} >>> >>> x1.symmetric_difference_update(['qux', 'corge']) >>> x1 {'bar', 'corge'}
add 元素添加到集合中
>>> x = {'foo', 'bar', 'baz'} >>> x.add('qux') >>> x {'bar', 'baz', 'foo', 'qux'}
remove 集合中移除一個(gè)元素
>>> x = {'foo', 'bar', 'baz'} >>> x.remove('baz') >>> x {'bar', 'foo'} # 如果元素步存在則引發(fā)異常 >>> x.remove('qux') Traceback (most recent call last): File "<pyshell#58>", line 1, in <module> x.remove('qux') KeyError: 'qux'
discard 集合中移除一個(gè)元素
>>> x = {'foo', 'bar', 'baz'} >>> x.discard('baz') >>> x {'bar', 'foo'} >>> x.discard('qux') >>> x {'bar', 'foo'}
pop 集合中移除一個(gè)隨機(jī)元素
>>> x = {'foo', 'bar', 'baz'} >>> x.pop() 'bar' >>> x {'baz', 'foo'} >>> x.pop() 'baz' >>> x {'foo'} >>> x.pop() 'foo' >>> x set() >>> x.pop() Traceback (most recent call last): File "<pyshell#82>", line 1, in <module> x.pop() KeyError: 'pop from an empty set'
clear 清空集合
>>> x = {'foo', 'bar', 'baz'} >>> x {'foo', 'bar', 'baz'} >>> >>> x.clear() >>> x set()
被凍結(jié)集合
freezeset 為 Python的內(nèi)置類型,不可變、不可操作。
>>> x = frozenset(['foo', 'bar', 'baz']) >>> x frozenset({'foo', 'baz', 'bar'}) >>> len(x) 3 >>> x & {'baz', 'qux', 'quux'} frozenset({'baz'})
嘗試修改 freezeset 的方法會(huì)失敗
>>> x = frozenset(['foo', 'bar', 'baz']) >>> x.add('qux') Traceback (most recent call last): File "<pyshell#127>", line 1, in <module> x.add('qux') AttributeError: 'frozenset' object has no attribute 'add' >>> x.pop() Traceback (most recent call last): File "<pyshell#129>", line 1, in <module> x.pop() AttributeError: 'frozenset' object has no attribute 'pop' >>> x.clear() Traceback (most recent call last): File "<pyshell#131>", line 1, in <module> x.clear() AttributeError: 'frozenset' object has no attribute 'clear' >>> x frozenset({'foo', 'bar', 'baz'})
以上就是Python必備技巧之集合Set的使用的詳細(xì)內(nèi)容,更多關(guān)于Python集合Set的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
通過(guò)Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的html頁(yè)面
這篇文章主要介紹了通過(guò)Python寫一個(gè)簡(jiǎn)單的html頁(yè)面,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05Python代碼實(shí)現(xiàn)找到列表中的奇偶異常項(xiàng)
這篇文章主要介紹了Python代碼實(shí)現(xiàn)找到列表中的奇偶異常項(xiàng),文章內(nèi)容主要利用Python代碼實(shí)現(xiàn)了從輸入列表中尋找奇偶異常項(xiàng),需要的朋友可以參考一下2021-11-11Python生成隨機(jī)數(shù)組的方法小結(jié)
這篇文章主要介紹了Python生成隨機(jī)數(shù)組的方法,結(jié)合實(shí)例形式總結(jié)分析了Python使用random模塊生成隨機(jī)數(shù)與數(shù)組操作相關(guān)技巧,需要的朋友可以參考下2017-04-04Python+streamlit實(shí)現(xiàn)輕松創(chuàng)建人事系統(tǒng)
streamlit 是 基于 Python 的一個(gè)非常強(qiáng)大的 web 構(gòu)建系統(tǒng),通過(guò)該類庫(kù),我們可以實(shí)現(xiàn)不需要編寫一行前端代碼而構(gòu)建一個(gè)完整的 Web 應(yīng)用。下面我們就來(lái)編寫一個(gè)簡(jiǎn)單的人事系統(tǒng)吧2023-02-02解決Jupyter NoteBook輸出的圖表太小看不清問(wèn)題
這篇文章主要介紹了解決Jupyter NoteBook輸出的圖表太小看不清問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04合并百度影音的離線數(shù)據(jù)( with python 2.3)
這篇文章主要介紹了合并百度影音的離線數(shù)據(jù)( with python 2.3)的相關(guān)資料2015-08-08Python matplotlib 動(dòng)畫繪制詳情
這篇文章主要介紹了Python matplotlib 動(dòng)畫繪制,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-09-09Python線程協(xié)作threading.Condition實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Python線程協(xié)作threading.Condition實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03