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 # -*- ...
随机推荐
- AndroidTouch事件总结
1.自定义的控件几乎都要用到触摸事件,不交互怎么响应,相关的事件处理函数由dispatchTouchEvent.onInterceptTouchEvent.onTouchEvent,处理这些事件的由v ...
- 自定义BadgeView
-(instancetype)initWithFrame:(CGRect)frame{ if (self=[super initWithFrame:frame]) { self.u ...
- Scala HandBook
目录[-] 1. Scala有多cool 1.1. 速度! 1.2. 易用的数据结构 1.3. OOP+FP 1.4. 动态+静态 1.5. DSL 1.6 ...
- (转)如何用Maven创建web项目(具体步骤)
原文链接:http://blog.csdn.net/chuyuqing/article/details/28879477 使用eclipse插件创建一个web project 首先创建一个Maven的 ...
- spider_jpg
__author__ = 'sus' #urllib模块提供了读取Web页面数据的接口import urllib#re模块主要包含了正则表达式import re#定义一个getHtml()函数def ...
- OC--编码建议
原文 http://www.cocoachina.com/ios/20151118/14242.html 本文是投稿文章,作者: IOS_Tips(微信公众号) “神在细节之中” Object ...
- angular js 图片轮播
搬运工: eg1: Build a Sweet AngularJS Photo Slider Pt 2 with ngTouch DEMO:http://paul-xiao.github.io/ang ...
- 常见JDBC连接数据库字符串
1.Mysql 驱动类:com.mysql.jdbc.Driver 连接字符串:jdbc:mysql://localhost:3306/dbname 2.Oracle 驱动类:oracle.jdbc. ...
- 查询oracle版本信息
select * from v$version;select * from v$instance;select * from nls_database_parameters;select * from ...
- DataTable 删除列 调整列顺序 修改列标题名称
DataTable dt = new DataTable(); //删除列 dt.Columns.Remove("Sex"); dt.Columns.Remove("Ag ...