python集合
集合(英文名 set),它是一個無序的不重復元素序列。
這里面有兩個重點:
無序,
不重復
1. 創建集合
集合的創建有兩種方法
第一種方法:使用 花括號 {} 直接創建,創建的時候,{} 可以包含有重要的元素,但是創建完后,集合會去重,只留第一個。
>>> aset = {"Apple", "Huawei", "Xiaomi"}
>>> aset
set(['Huawei', 'Xiaomi', 'Apple'])
第二種方法:使用 set() 方法進行創建,當set() 函數不接任何參數時,創建的是空集合,如果不創建空集合,可以傳入一個列表。
>>> bset = set() # 空集合
>>> bset
set([])
>>> cset = set(["Apple", "Huawei", "Xiaomi"])
>>> cset
set(['Huawei', 'Apple', 'Xiaomi'])
2. 增刪改查
增加元素
使用 add 函數可以往集合中傳入函數
>>> aset = set()
>>> aset
set([])
>>> aset.add("Apple")
>>> aset.add("Huawei")
>>> aset
set(['Huawei', 'Apple'])
另外,還可以使用 update 函數,來往集合中添加元素,update 函數后可接集合,列表,元組,字典等。
這是接集合的例子
>>> aset = set()
>>> aset
set([])
>>>
>>> # 接集合
>>> aset.update({"Apple"})
>>> aset
set(['Apple'])
>>>
>>> # 接列表
>>> aset.update(["Huawei"])
>>> aset
set(['Huawei', 'Apple'])
>>>
>>> # 接元組
>>> aset.update(("Xiaomi",))
>>> aset
set(['Huawei', 'Apple', 'Xiaomi'])
>>>
>>> # 接字典
>>> aset.update({"VIVO": "xxxx"})
>>> aset
set(['Huawei', 'Apple', 'VIVO', 'Xiaomi'])
刪除元素
使用 remove 函數可以刪除集合中的元素
>>> aset = {"Apple", "Huawei", "Xiaomi"}
>>> aset.remove("Xiaomi")
>>> aset
set(['Huawei', 'Apple'])
使用 remove 函數,如果對應的元素不存在,是會報錯的。
>>> aset = {"Apple", "Huawei", "Xiaomi"}
>>> aset.remove("VIVO")
Traceback (most recent call last):
File "", line 1, in
KeyError: 'VIVO'
對于這種情況,你可以使用 discard 函數,存在元素則移除,不存在也不會報錯。
>>> aset = {"Apple", "Huawei", "Xiaomi"}
>>> aset.discard("VIVO")
>>> aset
set(['Huawei', 'Xiaomi', 'Apple'])
此外,還有一個 pop 函數,用于從集合中隨機刪除元素,和列表、字典的 pop 不一樣,這里的 pop 不能加任何的參數。
>>> aset = {"Apple", "Huawei", "Xiaomi"}
>>> aset.pop()
'Huawei'
>>> aset.pop()
'Xiaomi'
>>> aset.pop()
'Apple'
最后,還要介紹一個 clear 函數,它用于清空集合的元素。
>>> aset = {"Apple", "Huawei", "Xiaomi"}
>>> aset
set(['Huawei', 'Xiaomi', 'Apple'])
>>> aset.clear()
>>> aset
set([])
修改元素
文章開頭處,已經說明了集合是 無序 的,因此集合是沒有索引的。
既然沒有索引,修改也無從談起。
記住:集合只有添加元素、刪除元素。
查詢元素
同上,沒有順序,也就沒有索引,沒有索引,查詢也無從談起。
但是我們可以查看集合的其他內容
比如,查看集合的長度
>>> aset = {"Apple", "Huawei", "Xiaomi"}
>>> len(aset)
3
3. 集合運算
求合集
將兩個集合進行合并并去重,可以使用 union 函數,下面的示例中,由于 Huawei 是重復的元素,只會保留一個。
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset.union(bset)
set(['Huawei', 'Apple', 'Xiaomi'])
另外還可以使用 | 的操作符
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset | bset
set(['Huawei', 'Apple', 'Xiaomi'])
求差集
要找出存在集合 A 但是不存在 集合 B 的元素,就是對兩個集合求差集。
可以使用 difference 函數,下面的示例中, Apple 在 aset 中存在,但在 bset 中不存在。
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset.difference(bset)
set(['Apple'])
另外還可以使用 - 的操作符,更加直觀
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset - bset
set(['Apple'])
求交集
要找出存在集合 A 并且存在集合 B 的元素,就是對兩個集合求交集。
可以使用 intersection 函數
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset.intersection(bset)
set(['Huawei'])
>>>
和 intersection 相似的還有一個 intersection_update 函數,它們的區別是,intersection_update 會原地更新在 aset 上,而不是會回交集。
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset.intersection_update(bset)
>>> aset
set(['Huawei'])
另外還可以使用 & 的操作符
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset & bset
set(['Huawei'])
求不重合集
如果計算兩個集合中不重復的元素集合,可以使用 symmetric_difference 函數
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset.symmetric_difference(bset)
set(['Xiaomi', 'Apple'])
和 symmetric_difference 相似的還有一個 symmetric_difference_update 函數,它們的區別是,symmetric_difference_update 會原地更新在 aset 上,而不是直接返回。
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset.symmetric_difference_update(bset)
>>> aset
set(['Apple', 'Xiaomi'])
4. 集合判斷
判斷是否有某元素
>>> aset = {"Apple", "Huawei"}
>>> "Apple" in aset
True
判斷兩集合是否有相同元素
如果兩集合有相同元素,則返回 False,如果沒有相同元素,則返回 True
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Xiaomi", "Huawei"}
>>> aset.isdisjoint(bset)
False
判斷是否是子集
>>> aset = {"Apple", "Huawei"}
>>> bset = {"Huawei"}
>>> bset.issubset(aset)
True
審核編輯:符乾江
-
集合
+關注
關注
0文章
9瀏覽量
8263 -
python
+關注
關注
57文章
4876瀏覽量
90025
發布評論請先 登錄
安裝 Python VisionFive_GPIO失敗是哪里出了問題?
沒有專利的opencv-python 版本
Termux中調試圣誕樹Python代碼
Python調用API教程
??FourCastNet 3實現快速精準的大型集合天氣預報
termux調試python猜數字游戲
termux如何搭建python游戲
python app不能運行怎么解決?
基礎篇3:掌握Python中的條件語句與循環
DLP4710EVM-LC燒入條紋集合,相機同步拍照時出現錯誤
如何破解GPU集群集合通信路徑的“黑盒”難題?
python入門圣經-高清電子書(建議下載)
?如何在虛擬環境中使用 Python,提升你的開發體驗~
零基礎入門:如何在樹莓派上編寫和運行Python程序?
python集合是什么
評論