網(wǎng)站首頁 編程語言 正文
集合類型— set, frozenset
set 對象是由具有唯一性的hashable 對象所組成的無序多項集。常見的用途包括成員檢測、從序列中去除重復項以及數(shù)學中的集合類計算,例如交集、并集、差集與對稱差集等等
兩個類的構造器具有相同的作用方式:
- class set([iterable ])
- class frozenset([iterable ])
集合可用多種方式來創(chuàng)建:
- 使用花括號內(nèi)以逗號分隔元素的方式: {‘jack’, ‘sjoerd’}
- 使用集合推導式: {c for c in ‘a(chǎn)bracadabra’ if c not in ‘a(chǎn)bc’}
- 使用類型構造器: set(), set(‘foobar’), set([‘a(chǎn)’, ‘b’, ‘foo’])
set 和frozenset 的實例提供以下操作:
len(s)
計算集合 s 元素個數(shù)
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)
返回兩個集合的并集,即包含了所有集合的元素,重復的元素只會出現(xiàn)一次
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)
用于返回集合的差集,即返回的集合元素包含在第一個集合中,但不包含在第二個集合(方法的參數(shù))中。
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)
用于修改當前集合,可以添加新的元素或集合到當前集合中,如果添加的元素在集合中已存在,則該元素只會出現(xiàn)一次,重復的會忽略。
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() 方法的區(qū)別在于 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)
用于給集合添加元素,如果添加的元素在集合中已存在,則不執(zhí)行任何操作。
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()
從集合中移除并返回任意一個元素。如果集合為空則會引發(fā)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
相關推薦
- 2022-01-11 web:war 和 web:war exploded 遇到的坑
- 2022-05-15 uniapp穿透第三方uView組件樣式
- 2022-10-02 Android實現(xiàn)倒計時的方案梳理_Android
- 2022-11-17 Python中的優(yōu)先隊列(priority?queue)和堆(heap)_python
- 2022-05-27 C++的STL中accumulate函數(shù)的使用方法_C 語言
- 2023-05-08 淺談golang通道類型_Golang
- 2022-04-30 詳解DataGridView控件的數(shù)據(jù)綁定_C#教程
- 2022-10-13 python中arrow庫用法大全_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支