Python 基礎 - 集合的使用
集合是一個無序的,不重複的數據組合,主要的作用如下
去重
,把一個列表變成集合,就會自動去重了。關係測試
,測試二組數據之前的交集、差集、聯集等關係。
接下來我們來實作看看什麼是去重
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
print(list_1, type(list_1))
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} <class 'set'>
Process finished with exit code 0
觀察一下,發現原本有重複出現的數字已經不見了,而且這個列表也已經變成一個集合了
接下來我們來試試關係測試
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
print(list_1, list_2)
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
Process finished with exit code 0
觀察上面代碼,這二個集合中,有沒有二個一樣的數字?那如何將這二個一樣的數字給取出來呢?
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
print(list_1, list_2)
print(list_1.intersection(list_2)) # 交集
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
{4, 6}
Process finished with exit code 0
唔…成功取出來了,{4, 6}
就是這二個集合的交集
,所謂交集
就是二個集合裡面都有的東西,A和B的交集寫作A ∩ B
如果做二個集合的聯集
要怎麼取呢?
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
print(list_1, list_2)
print(list_1.union(list_2)) # 聯集
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
{0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22}
Process finished with exit code 0
唔…在觀察一下,發現這二個集合被合併成一個集合了,並且也做了去重
,這個就叫做聯集
, A和B的聯集通常寫作 A ∪ B
如果做二個集合的差集
要怎麼取呢?
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
print(list_1, list_2)
print(list_1.difference(list_2)) # 差集 in list_1 but not in list_2
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
{1, 3, 5, 9, 7}
Process finished with exit code 0
觀察一下,可以想像成把list_1這個集合減去list_1跟list_2的交集
,就會是list_1的差集
了,那…list_2
的差集會是長什麼樣子呢?
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
print(list_1, list_2)
print(list_1.difference(list_2)) # 差集 in list_1 but not in list_2
print(list_2.difference(list_1))
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
{1, 3, 5, 9, 7}
{0, 8, 2, 66, 22}
Process finished with exit code 0
唔…在仔細觀察一下,list_2的差集有什麼不同!是不是也發現{4, 6}
這二個數字也不見了,只保留了{0, 8, 2, 66, 22}
除了這三個之外,還有沒有別的關係?是有的
接下來我們來試試子集
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
print(list_1, list_2)
print(list_1.issubset(list_2)) # 子集
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
False
Process finished with exit code 0
咦,出現False
,為什麼會出現false呢?
是因為list_1這個集合裡的數字,沒有完全符合list_2這個集合裡的數字,所以才會是False
,那有子集就會有父集
,那就來試試看list_2是不是list_1的父集?
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
print(list_1, list_2)
print(list_1.issubset(list_2)) # 子集
print(list_2.issuperset(list_1)) # 父集
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
False
False
Process finished with exit code 0
接下來我們來新增一個集合list_3
,再試試剛剛那個子集
跟父集
,觀察一下,有什麼不同?
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])
print(list_1, list_2, list_3)
print(list_3.issubset(list_1)) # 子集
print(list_1.issuperset(list_3)) # 父集
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
True
True
Process finished with exit code 0
唔…list_3
是list_1
的子集,反過來說,list_1
是list_3
的父集
再來試試對稱差集
,觀察一下看看有什麼不同
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])
print(list_1, list_2, list_3)
print(list_1.symmetric_difference(list_2))
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
{0, 1, 2, 66, 3, 5, 7, 8, 9, 22}
Process finished with exit code 0
唔,就是把二個集合裡所沒有的元素給取出來,所以就取出了{0, 1, 2, 66, 3, 5, 7, 8, 9, 22}
,而{4, 6}
是這二個集合都有的,所以就不取了
再來我們在新增一個集合叫list_4
,當二個集合沒有交集的話,要怎麼判斷?
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])
list_4 = set([5, 6, 8])
print(list_1, list_2, list_3)
print(list_3.isdisjoint(list_4)) # Return True if two sets have a null intersection.
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
True
Process finished with exit code 0
唔,有發現結果回應True
,就代表set.isdisjoint()
是判斷當二個集合沒有交集
時,就返回True
,那我在修改一下list_4
,在觀察一下
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])
list_4 = set([5, 6, 7, 8])
print(list_1, list_2, list_3)
print(list_3.isdisjoint(list_4))
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
False
Process finished with exit code 0
嗯!結果返回一個False
,就代表list_3
跟list_4
是有交集
的
用符號來表示交集
、聯集
、差集
、對稱差集
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])
list_4 = set([5, 6, 7, 8])
print(list_1, list_2, list_3)
print(list_1 & list_2) # 交集(intersection)
print(list_1 | list_2) # 聯集(Union)
print(list_1 - list_2) # 差集(difference) in list_1 not in list_2
print(list_1 ^ list_2) # 對稱差集(symmetric_difference)
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
{4, 6}
{0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22}
{1, 3, 5, 9, 7}
{0, 1, 2, 66, 3, 5, 7, 8, 9, 22}
Process finished with exit code 0
再來我們來操作集合的新增、修改、刪除,先試試對一個集合做新增
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_1.add(44)
print(list_1)
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 9, 44}
Process finished with exit code 0
再來新增多個數字到集合裡
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_1.add(44)
list_1.update([9527, 520, 1314])
print(list_1)
---------------執行結果---------------
{1, 1314, 3, 4, 5, 6, 7, 520, 9, 44, 9527}
Process finished with exit code 0
再來試試刪除的方法
Method 1: set.remove() 刪除元素,但刪除一個不存在的元素,會噴error
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_1.add(44)
list_1.update([9527, 520, 1314])
list_1.remove(1314)
print(list_1)
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 520, 9, 44, 9527}
Process finished with exit code 0
Method 2: set.pop() 隨機任意刪,並且打印出刪除的元素
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_1.add(44)
list_1.update([9527, 520, 1314])
list_1.remove(1314)
print(list_1)
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1)
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 520, 9, 44, 9527}
1
3
4
5
6
7
520
{9, 44, 9527}
Process finished with exit code 0
Method 3: set.discard() 如果元素存在,就刪除,元素不存在,也不會噴error
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_1.add(44)
list_1.update([9527, 520, 1314])
list_1.remove(1314)
print(list_1)
list_1.discard(9)
list_1.discard(999) # 故意刪除一個不存在的,也不會報錯
print(list_1)
---------------執行結果---------------
{1, 3, 4, 5, 6, 7, 520, 9, 44, 9527}
{1, 3, 4, 5, 6, 7, 520, 44, 9527}
Process finished with exit code 0
計算集合的長度
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)
list_1.add(44)
list_1.update([9527, 520, 1314])
list_1.remove(1314)
print(len(list_1))
print(list_1)
---------------執行結果---------------
10
{1, 3, 4, 5, 6, 7, 520, 9, 44, 9527}
Process finished with exit code 0
參考資料:
Python 基礎 - 集合的使用的更多相关文章
- Python 基礎 - for流程判斷
今天介紹另一個循環判斷式 for循環,首先,先寫一個很簡單的 for循環的代碼 #!/usr/bin/env python3 # -*- coding:utf-8 -*- for i in range ...
- Python 基礎 - if else流程判斷
hmm~前面講了那麼多,終於可以稍稍的正式進入另一個階段,沒錯,要開始寫判斷式了 這次先從最簡單的判斷式開始,if else 開始- Go 首先,之前有寫有一個簡單的互動式 用戶輸入 的代碼,忘記了嗎 ...
- Python 基礎 - 數據類型
標準數據類型 Python3 中有六個標準的數據類型 1 Number(數字) 2 String(字符串) 3 List (列表) 4 Tuple (元組) 5 Sets (集合) 6 Diction ...
- Python 基礎 - 字符編碼
Python 解釋器在加載 .py 文件中的代碼時,會對內容進行編碼 (默認 ascill) ASCII (American Standard Code for Information Interch ...
- Python 基礎 - pyc 是什麼
Python2.7 版中,只要執行 .py 的檔案後,即會馬上產生一個 .pyc 的檔案,而在 Python3 版中,執行 .py 的檔案後,即會產生一個叫 __pycache__ 的目錄,裡面也會有 ...
- Python 基礎 - 字符轉編碼操作
回顧字符編碼的前世今生 ASCII 只能儲英文或特殊字符,只占一個字節,一個字節8bit,不能儲中文,所以才出現Unicode Unicode 不管是中文或英文,都是占二個字節,一個字節8bit UT ...
- Python 基礎 - 文件操作_v2
嗯,那如何要把游標的位置給打印來? #!/usr/bin/env python3 # -*- coding:utf-8 -*- f = open('test', 'r') print(f.tell() ...
- Python 基礎 - 文件的操作
在來我們來玩一下文件操作,這個在未來工作上,也是會很常用到的功能 Python2.7中,可以用file()來打開文件,而在Python3中,一律都是用open(),接下來在當前目錄下,先建立一個空文件 ...
- Python 基礎 - 字典的操作使用
接下來介紹字典,這在未來工作上,會是很常使用的,就來好好了解一下唄- 字典是一個 key(鍵)-value(值) 的數據類型,可以儲存很多訊息 #!/usr/bin/env python3 # -*- ...
随机推荐
- Sql Server xml 类型字段的增删改查
1.定义表结构 在MSSM中新建数据库表CommunicateItem,定义其中一个字段ItemContentXml 为xml类型 2.编辑表数据,新增一行,发现xml类型不能通过设计器录入数据. 需 ...
- 在heroku上部署gost代理服务端
gost ( https://github.com/ginuerzh/gost ) gost 的 websocket tunnel 功能使用 WebSocket 协议,相较 HTTP 协议能做更多事, ...
- python 邮件
1:文件形式的邮件 01.#!/usr/bin/env python3 02.#coding: utf-8 03.import smtplib 04.from email.mime.text impo ...
- Newtonsoft.Json 序列化和反序列化 时间格式
From : http://www.cnblogs.com/litian/p/3870975.html 1.JSON序列化 string JsonStr= JsonConvert.SerializeO ...
- 《Pro Express.js》学习笔记——app.params中间件
app.param中间件用于对URL中的参数进行获取.加工.输出,提供公有逻辑,以达到代码重构的目的. 以下示例采取三个步骤对代码进行重构,app.param中间件的作用非常明显: 不使用中间件 使用 ...
- Mysql VARCHAR的最大长度到底是多少
MySQL 数据库的varchar类型在4.1以下的版本中的最大长度限制为255,其数据范围可以是0~255或1~255(根据不同版本数据库来定).在 MySQL5.0以上的版本中,varchar数据 ...
- 查找“asdfjvjadsffvaadfkfasaffdsasdffadsafafsafdadsfaafd” 该字符串中有多少个af
package lovo.bean; import java.util.Scanner; public class Java { @param args public static void main ...
- NFS网络文件系统安装配置
简介 NFS(Network File System)FS是由Sun开发并发展起来的一项用于在不同机器,不同操作系统之间通过网络共享数据.客户端用mount命令把远程的NFS文件系统挂载到本地即可,操 ...
- ubuntu的一些操作
1.修改ubuntu的grub启动选择菜单 需要修改到文件为 /boot/grub/grub.cfg 命令: sudo gedit /boot/grub/grub.cfg 修改默认启动项:set de ...
- 存到cookie里能提高性能吗?
今天刚刚看了篇微信,大体意思是说g哥之类的网站把很多存session的东西都放cookie里了,可减少服务器的负担种种.然后我就发现我对request到application的记忆有些模糊了,哪些是在 ...