Python集合之set和frozenset的使用詳解
簡介
集合對象 set 是由具有唯一性的可哈希對象組成的無序多項集,如 list 不能哈希因此,不能作為 set 的一項。
set 的常見用途包括成員檢測、從序列中去除重復(fù)項以及數(shù)學(xué)中的集合類計算,如交集、并集、差集與對稱差集等。
set 不記錄元素位置或插入順序。 相應(yīng)地,set 不支持索引、切片或其他序列操作。
目前有兩種內(nèi)置集合類型,set 和 frozenset:
- set 是可變的,其內(nèi)容可以通過 add() 和 remove() 來改變。由于是可變類型,所以不可哈希值,則不能作為字典的鍵或其他集合的元素。
- frozenset 是不可變的,所以可哈希,因此可以用為字典的鍵或其他集合的元素。
除了可以使用 set 構(gòu)造器,非空的 set (不是 frozenset) 還可以通過將以逗號分隔的元素列表包含于花括號之內(nèi)來創(chuàng)建,例如: {‘jack’, ‘sjoerd’}。
構(gòu)造
花括號內(nèi)用逗號分隔
集合推導(dǎo)式
類型構(gòu)造器
a = {1, 2, 3} # 花括號內(nèi)用逗號分隔 b = {i for i in range(4) if i > 0} # 集合推導(dǎo)式 c = set([1, 2, 3]) # 類型構(gòu)造器 print(a, type(a)) print(b, type(b)) print(c, type(c)) # {1, 2, 3} <class 'set'> # {1, 2, 3} <class 'set'> # {1, 2, 3} <class 'set'>
基本使用
a = {1, 2, 3} print(len(a)) # 元素數(shù)量 print(1 in a) # 檢測成員 print(1 not in a) # 檢測成員 # 3 # True # False
交集、并集、差集、對稱差集
A = {1, 2, 3} B = {3, 4, 5} print(A.intersection(B)) # 交集 print(A.union(B)) # 并集 print(A.difference(B)) # 差集 print(A.symmetric_difference(B)) # 對稱差集 # {3} # {1, 2, 3, 4, 5} # {1, 2} # {1, 2, 4, 5}
無交集、子集、超集
- 與other無交集:
isdisjoint(other)
- 是other的子集:
issubset(other)
,相當(dāng)于set <= other
- 是other的超集:
issuperset(other)
,相當(dāng)于set >= other
A = {1, 2, 3} B = {3, 4, 5} C = {1, 2, 3, 4, 5, 6} D = {7, 8, 9} # 是否無交集 print(A.isdisjoint(A)) # False print(A.isdisjoint(B)) # False print(A.isdisjoint(C)) # False print(A.isdisjoint(D)) # True print() # 是否子集 print(A.issubset(A)) # True print(A.issubset(B)) # False print(A.issubset(C)) # True print(A.issubset(D)) # False print() # 是否超集 print(C.issuperset(A)) # True print(C.issuperset(B)) # True print(C.issuperset(C)) # True print(C.issuperset(D)) # False
運算符
A、B、C 是 C 的子集,C 是 A、B、C 的超集
A、B 是 C 的真子集,C 是 A、B 的真超集
運算符 | 含義 |
---|---|
<= | 子集 |
< | 真子集 |
>= | 超集 |
> | 真超集 |
& | 交集 |
| | 并集 |
- | 差集 |
^ | 對稱差集 |
A = {1, 2, 3} B = {3, 4, 5} C = {1, 2, 3, 4, 5, 6} D = {7, 8, 9} # 子集,相當(dāng)于issubset(other) print(A <= C) # True print(B <= C) # True print(C <= C) # True print(D <= C) # False print() # 真子集 print(A < C) # True print(B < C) # True print(C < C) # False print(D < C) # False print() # 超集,相當(dāng)于issuperset(other) print(C >= A) # True print(C >= B) # True print(C >= C) # True print(C >= D) # False print() # 真超集 print(C > A) # True print(C > B) # True print(C > C) # False print(C > D) # False print() # 交集,相當(dāng)于intersection(*other) print(A & B) # {3} print(A.intersection(B)) # {3} # 并集,相當(dāng)于union(*other) print(A | B) # {1, 2, 3, 4, 5} print(A.union(B)) # {1, 2, 3, 4, 5} # 差集,相當(dāng)于difference(*other) print(A - B) # {1, 2} print(A.difference(B)) # {1, 2} # 對稱差集,相當(dāng)于symmetric_difference(other) print(A ^ B) # {1, 2, 4, 5} print(A.symmetric_difference(B)) # {1, 2, 4, 5}
可用于 set 的操作
不可用于不可變的 frozenset
操作 | 含義 |
---|---|
add(elem) | 添加單個元素 |
remove(elem) | 移除單個元素,如果不存在報錯 |
discard(elem) | 移除單個元素,如果存在 |
pop() | 移除并返回任一元素,集合為空報錯 |
clear() | 移除所有元素 |
update(*others) set |= other | 添加元素 |
difference_update(*others) set -= other | 移除元素 |
symmetric_difference_update(other) set ^= other | 移除相同元素 |
A = {1, 2, 3} A.add(4) # 添加單個元素 print(A) # {1, 2, 3, 4} A = {1, 2, 3} A.remove(3) # 移除單個元素,如果不存在報錯 print(A) # {1, 2} A = {1, 2, 3} A.discard(3) # 移除單個元素,如果存在 A.discard(99) # 移除單個元素,如果存在 print(A) # {1, 2} A = {2, 1, 3} print(A.pop()) # 移除并返回任一元素,集合為空報錯 A = {1, 2, 3} A.clear() # 移除所有元素 print(A) # set()
A = {1, 2, 3} B = {3, 4, 5} A.update(B) # 添加元素 # A |= B # 添加元素 print(A) # {1, 2, 3, 4, 5} A = {1, 2, 3} B = {3, 4, 5} A.difference_update(B) # 移除元素 # A -= B # 移除元素 print(A) # {1, 2} A = {1, 2, 3} B = {3, 4, 5} A.symmetric_difference_update(B) # 移除相同元素 # A ^= B # 移除相同元素 print(A) # {1, 2, 4, 5}
以上就是Python集合之set和frozenset的使用詳解的詳細內(nèi)容,更多關(guān)于Python集合set frozenset的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python-flask調(diào)用接口返回中文數(shù)據(jù)問題
這篇文章主要介紹了Python-flask調(diào)用接口返回中文數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03如何使用VSCode愉快的寫Python于調(diào)試配置步驟
從我的使用經(jīng)驗出發(fā),可以說VSCode用來寫Python真的是再合適不過了,你將體驗到絲滑的編程體驗和無限擴展的可能。而且,如果你的項目是包含多種語言的,比如Web開發(fā),你不必再開多個編輯器和其他工具,因為這一切都可以在VSCode里完成了2018-04-04python機器學(xué)習(xí)基礎(chǔ)決策樹與隨機森林概率論
這篇文章主要為大家介紹了python機器學(xué)習(xí)基礎(chǔ)決策樹與隨機森林概率論詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-11-11python之語句mode = 'test' if y&nb
這篇文章主要介紹了python之語句mode = 'test' if y is None else 'train'問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02PyCharm鼠標右鍵不顯示Run unittest的解決方法
今天小編就為大家分享一篇PyCharm鼠標右鍵不顯示Run unittest的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-11-11