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 # -*- ...
随机推荐
- Oracle连接数据库的封装类OracleDB
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.S ...
- WPF中Grid布局
WPF中Grid布局XMAl与后台更改,最普通的登录界面为例. <Grid Width="200" Height="100" > <!--定义 ...
- Web Api 多项目文档生成之SwaggerUI
SwaggerUI 可以生成不错的文档,但默认只能作用于单个api 项目,研究了一下源码发现只需修改一下SwaggerConfig.cs文件即可支持多API项目 1.使用生成脚本把xml文件复制到AP ...
- javadoc 生成自定义的标签
转自:http://www.blogjava.net/lishunli/archive/2010/01/12/309218.html Technorati 标记: tools 关键词 个性化地生成Ja ...
- ORA-06552: PL/SQL: Compilation unit analysis terminated ORA-06553: PLS-553: character set name is not recognized
首先,确认字符集是否修改的不彻底.SELECT DISTINCT (NLS_CHARSET_NAME(CHARSETID)) CHARACTERSET,DECODE(TYPE#, 1, DECODE( ...
- static的作用
在C语言中,static的字面意思很容易把我们导入歧途,其实它的作用有三条. (1)先来介绍它的第一条也是最重要的一条:隐藏. 当我们同时编译多个文件时,所有未加static前缀的全局变量和函数都具有 ...
- sql语句的匹配
like 的通配符有两种 %(百分号):代表零个.一个或者多个字符. _(下划线):代表一个数字或者字符. 1. name以"李"开头 where name like '李%' 2 ...
- 如何在tomcat中如何部署java EE项目
如何在tomcat中如何部署java EE项目 1.直接把项目复制到Tomcat安装目录的webapps目录中,这是最简单的一种Tomcat项目部署的方法,也是初学者最常用的方法.2.在tomcat安 ...
- Java网络编程及安全
一.实验内容: 1.运行教材上TCP代码,结对进行,一人服务器,一人客户端: 2.利用加解密代码包,编译运行代码,一人加密,一人解密: 3.集成代码,一人加密后通过TCP发送: 注:加密使用AES或者 ...
- android基础(六)android的消息处理机制
Android中的消息处理机制由四个部分组成:Message.Handler.MessageQueue和Looper,并且MessageQueue封装在Looper中,我们一般不直接与MQ打交道. 一 ...