網站首頁 編程語言 正文
集合類型— set, frozenset
set 對象是由具有唯一性的hashable 對象所組成的無序多項集。常見的用途包括成員檢測、從序列中去除重復項以及數學中的集合類計算,例如交集、并集、差集與對稱差集等等
兩個類的構造器具有相同的作用方式:
- class set([iterable ])
- class frozenset([iterable ])
集合可用多種方式來創建:
- 使用花括號內以逗號分隔元素的方式: {‘jack’, ‘sjoerd’}
- 使用集合推導式: {c for c in ‘abracadabra’ if c not in ‘abc’}
- 使用類型構造器: set(), set(‘foobar’), set([‘a’, ‘b’, ‘foo’])
set 和frozenset 的實例提供以下操作:
len(s)
計算集合 s 元素個數
x in s
檢測x是否為s中的成員
x not in s
檢測x 是否非s 中的成員
isdisjoint(other)
用于判斷兩個集合是否包含相同的元素,如果沒有返回 True,否則返回 False
x = {"apple", "banana", "cherry"} y = {"google", "runoob", "facebook"} z = x.isdisjoint(y) print(z)
issubset(other)
用于判斷集合的所有元素是否都包含在指定集合中,如果是則返回 True,否則返回 False
x = {"a", "b", "c"} y = {"f", "e", "d", "c", "b", "a"} z = x.issubset(y)
issuperset(other)
用于判斷指定集合的所有元素是否都包含在原始的集合中,如果是則返回 True,否則返回 False。
x = {"f", "e", "d", "c", "b", "a"} y = {"a", "b", "c"} z = x.issuperset(y) print(z)
union(*others)
返回兩個集合的并集,即包含了所有集合的元素,重復的元素只會出現一次
x = {"apple", "banana", "cherry"} y = {"google", "runoob", "apple"} z = x.union(y) print(z)
intersection(*others)
用于返回兩個或更多集合中都包含的元素,即交集。
x = {"apple", "banana", "cherry"} y = {"google", "runoob", "apple"} z = x.intersection(y) print(z)
difference(*others)
用于返回集合的差集,即返回的集合元素包含在第一個集合中,但不包含在第二個集合(方法的參數)中。
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.difference(y) print(z)
symmetric_difference(other)
返回兩個集合中不重復的元素集合,即會移除兩個集合中都存在的元素。
x = {"apple", "banana", "cherry"} y = {"google", "runoob", "apple"} z = x.symmetric_difference(y) print(z)
copy()
用于拷貝一個集合。
sites = {"Google", "Runoob", "Taobao"} x = sites.copy() print(x)
可用于set 而不能用于不可變的frozenset 實例的操作:
update(*others)
用于修改當前集合,可以添加新的元素或集合到當前集合中,如果添加的元素在集合中已存在,則該元素只會出現一次,重復的會忽略。
x = {"apple", "banana", "cherry"} y = {"google", "runoob", "apple"} x.update(y) print(x)
intersection_update(*others)
- intersection_update() 方法用于獲取兩個或更多集合中都重疊的元素,即計算交集。
- intersection_update() 方法不同于 intersection() 方法,因為 intersection() 方法是返回一個新的集合,而 intersection_update() 方法是在原始的集合上移除不重疊的元素。
x = {"apple", "banana", "cherry"} # y 集合不包含 banana 和 cherry,被移除 y = {"google", "runoob", "apple"} x.intersection_update(y) print(x)
difference_update(*others)
- difference_update() 方法用于移除兩個集合中都存在的元素。
- difference_update() 方法與 difference() 方法的區別在于 difference() 方法返回一個移除相同元素的新集合,而 difference_update() 方法是直接在原來的集合中移除元素,沒有返回值。
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.difference_update(y) print(x)
symmetric_difference_update(other)
symmetric_difference_update() 方法移除當前集合中在另外一個指定集合相同的元素,并將另外一個指定集合中不同的元素插入到當前集合中
x = {"apple", "banana", "cherry"} y = {"google", "runoob", "apple"} x.symmetric_difference_update(y) print(x)
add(elem)
用于給集合添加元素,如果添加的元素在集合中已存在,則不執行任何操作。
fruits = {"apple", "banana", "cherry"} fruits.add("orange") print(fruits)
remove(elem)
用于移除集合中的指定元素。
fruits = {"apple", "banana", "cherry"} fruits.remove("banana") print(fruits)
discard(elem)
如果元素elem 存在于集合中則將其移除
fruits = {"apple", "banana", "cherry"} fruits.discard("banana") print(fruits)
pop()
從集合中移除并返回任意一個元素。如果集合為空則會引發KeyError。
fruits = {"apple", "banana", "cherry"} fruits.pop() print(fruits)
clear()
用于移除集合中的所有元素。
fruits = {"apple", "banana", "cherry"} fruits.clear() print(fruits)
關系運算
s_1024 = {"佩奇","老男孩","海峰","馬JJ","老村長","黑姑娘","Alex"} s_pornhub = {"Alex","Egon","Rain","馬JJ","Nick","Jack"} print(s_1024 & s_pornhub) # 交集, elements in both set print(s_1024 | s_pornhub) # 并集 or 合集 print(s_1024 - s_pornhub) # 差集 , only in 1024 print(s_pornhub - s_1024) # 差集, only in pornhub print(s_1024 ^ s_pornhub) # 對稱差集, 把腳踩2只船的人T出去
總結
原文鏈接:https://blog.csdn.net/sinat_28317385/article/details/123467456
- 上一篇:Git 克隆指定分支的代碼
- 下一篇:bat批處理之字符串操作的實現_DOS/BAT
相關推薦
- 2023-01-21 VmWare安裝Centos后配置Net網絡SSH鏈接問題及解決_VMware
- 2022-04-08 python如何去除異常值和缺失值的插值_python
- 2023-11-16 python 插值 —— 如何實現插值,以及錯誤ValueError: A value in x_n
- 2022-02-13 group?by用法詳解_oracle
- 2022-10-08 Python使用plt.boxplot()函數繪制箱圖、常用方法以及含義詳解_python
- 2022-09-09 Go語言中函數可變參數(Variadic?Parameter)詳解_Golang
- 2022-07-29 pytest解讀一次請求多個fixtures及多次請求_python
- 2024-01-06 RocketMQ消息丟失問題
- 最近更新
-
- 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同步修改后的遠程分支