網站首頁 編程語言 正文
簡介
集合對象 set 是由具有唯一性的可哈希對象組成的無序多項集,如 list 不能哈希因此,不能作為 set 的一項。
set 的常見用途包括成員檢測、從序列中去除重復項以及數學中的集合類計算,如交集、并集、差集與對稱差集等。
set 不記錄元素位置或插入順序。 相應地,set 不支持索引、切片或其他序列操作。
目前有兩種內置集合類型,set 和 frozenset:
- set 是可變的,其內容可以通過 add() 和 remove() 來改變。由于是可變類型,所以不可哈希值,則不能作為字典的鍵或其他集合的元素。
- frozenset 是不可變的,所以可哈希,因此可以用為字典的鍵或其他集合的元素。
除了可以使用 set 構造器,非空的 set (不是 frozenset) 還可以通過將以逗號分隔的元素列表包含于花括號之內來創建,例如: {‘jack’, ‘sjoerd’}。
構造
花括號內用逗號分隔
集合推導式
類型構造器
a = {1, 2, 3} # 花括號內用逗號分隔 b = {i for i in range(4) if i > 0} # 集合推導式 c = set([1, 2, 3]) # 類型構造器 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)) # 元素數量 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)
,相當于?set <= other
- 是other的超集:
issuperset(other)
,相當于?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} # 子集,相當于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() # 超集,相當于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() # 交集,相當于intersection(*other) print(A & B) # {3} print(A.intersection(B)) # {3} # 并集,相當于union(*other) print(A | B) # {1, 2, 3, 4, 5} print(A.union(B)) # {1, 2, 3, 4, 5} # 差集,相當于difference(*other) print(A - B) # {1, 2} print(A.difference(B)) # {1, 2} # 對稱差集,相當于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}
原文鏈接:https://xercis.blog.csdn.net/article/details/118487355
相關推薦
- 2022-12-01 Go語言實戰之實現一個簡單分布式系統_Golang
- 2022-04-21 C#使用Chart繪制曲線_C#教程
- 2022-10-29 【npm 報錯 gyp info it worked if it ends with ok 大概率是
- 2022-12-03 robocopy命令用法實例解析_DOS/BAT
- 2022-09-13 go語言中切片Slice與數組Array對比以及panic:?runtime?error:?inde
- 2022-10-15 Python基礎學習之函數和代碼復用詳解_python
- 2023-01-10 redis中Could?not?get?a?resource?from?the?pool異常及解決方
- 2023-07-07 什么是 Spring 框架?使用 Spring 框架的好處是什么?Spring 框架中用到了哪些設計
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支