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 # -*- ...
随机推荐
- android xfermode绘图
1.fermode:是来自 transfer-modes,在画笔中设置xfermode后,画笔在使用的时候就会应用设置的像素转换模式.就类似于ps中的选取功能,留下的选取是从原选区中去除还是合并,取交 ...
- 关于/usr/local/lib/libz.a(zutil.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC解决办法
具体报错截图如下: 解决方法: 题外话,我对makefill cmake也是一窍不通因此本人也是不想去积极的解决这个问题,但是当你求助无缘的时候你才会静心去思考.读到这句话的时候也许你已经发现了问题所 ...
- cron表达式使用详解
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...
- POJ 3415 Common Substrings 后缀数组+并查集
后缀数组,看到网上很多题解都是单调栈,这里提供一个不是单调栈的做法, 首先将两个串 连接起来求height 求完之后按height值从大往小合并. height值代表的是 sa[i]和sa[i ...
- Pythonn 内置函数
abs 绝对值 n = abs(-1) print(n) ========================= /usr/bin/python3.5 /home/liangml/pythonscript ...
- iOS程序 防止动态调试和代码注入
http://ruixiazun.blog.163.com/blog/static/9068791820141173588694/ iPhone真实的运行环境是没有sys/ptrace.h抛出.ptr ...
- IOS 学习 开发 自定义 UINavigationController 导航
文件目录如下:基本导航顺序: root -> First -> Second -> Third.其中,FirstViewController作为 navigation堆栈的rootv ...
- android学习之RadioButton和CheckBox
移通152 余继彪 RadioBuuton是一个单选按钮,CheckBox是一个复选按钮 . RadioButton的使用 ,首先要将RadioButton放在RadioGroup中,RadioGro ...
- 关于C#的内存释放问题
最近在做一个要循环读取xml文件的功能. 想法是读取一个文件,释放一个文件,这样就不会消耗太多内存. 结果运行起来内存不停涨,最后内存溢出.用gc,disponse,异步都没有用. 后来改成relea ...
- logistc regression练习(三)
% Exercise 4 -- Logistic Regression clear all; close all; clc x = load('E:\workstation\data\ex4x.dat ...