# Sets 集合 > [color=#40f1ef][name=LHB阿好伯, 2020/12/02][:earth_africa:](https://www.facebook.com/LHB0222/) ###### tags: `Python_30` `R & python` [TOC] 集合的數學定義也可以在python與R中應用 Sets是無序和未索引的不同元素的集合 在python中,集合用於存儲唯一項 並且可以在集合之間找到*和集,交集,差,子集,和不相交集*等資料 # 創建集合 ## 空集合 Python我們是使用 set()創建一個空集合或是利用大括弧 =={}== 建立 然而R則會使用c()來建立一個類似集合的資料 ```python= # Python code st = set() type(st) ``` :::success <class 'set'> ::: ## 創立集合 ```python=+ # Python code fruits = {'banana', 'orange', 'mango', 'lemon'} type(fruits) ``` :::success <class 'set'> ::: ```r= # R code fruits <- c('banana', 'orange', 'mango', 'lemon') ``` ### 添加集合項目 Python添加項目則是使用 ==A.add(B)== 與清單所使用的.append()不同 ```python= # Python code fruits = {'banana', 'orange', 'mango', 'lemon'} fruits.add('lime') fruits ``` :::success {'orange', 'lemon', 'mango', 'banana', 'lime'} ::: 或是使用 ==A.update(B)== 來添加多個項目 ```python=+ # Python code fruits = {'banana', 'orange', 'mango', 'lemon'} vegetables = ('tomato', 'potato', 'cabbage','onion', 'carrot') fruits.update(vegetables) fruits ``` :::success {'orange', 'tomato', 'lemon', 'mango', 'potato', 'onion', 'banana', 'cabbage', 'carrot'} ::: R則是一樣使用append()函數來添加 ```r= # R code fruits <- c('banana', 'orange', 'mango', 'lemon') vegetables <- c('tomato', 'potato', 'cabbage','onion', 'carrot') append(fruits,vegetables) ``` :::success [1] "banana" "orange" "mango" "lemon" "tomato" "potato" "cabbage" "onion" "carrot" ::: ### 從集合中刪除項目 Python可以使用.remove()方法從集合中刪除特定項目 一次只能刪除一個物件 ```python= # Python code fruits = {'banana', 'orange', 'mango', 'lemon'} vegetables = ( 'tomato', 'cabbage','onion', 'carrot') fruits.update(vegetables) fruits.remove( 'tomato',) fruits ``` :::success {'orange', 'lemon', 'mango', 'onion', 'banana', 'cabbage', 'carrot'} ::: R語言則一樣使用索引搭配關係運算子達成相同功能 ```r= # R code fruits <- c('banana', 'orange', 'mango', 'lemon') vegetables <- c('tomato', 'potato', 'cabbage','onion', 'carrot') x <- append(fruits,vegetables) x[x != 'tomato'] ``` :::success [1] "banana" "orange" "mango" "lemon" "potato" "cabbage" "onion" "carrot" ::: ## 刪除與清除集合 這部分其餘與[Lists清單](/vCN7TwuWRKW-QDmoznnbSA)的方法一樣就不再贅述 .clear() 清除 .pop() 刪除最後一項 del 刪除集合 ### 清單轉換成集合 使用set()可以將清單轉換成集合資料 ```python= fruits = ['banana', 'orange', 'mango', 'lemon','orange', 'banana'] type(fruits) ``` :::success <class 'list'> ::: ```python=+ fruits = set(fruits) type(fruits) ``` :::success <class 'set'> ::: # Joining Sets聯接集 ## Union聯集 Python 可以使用.union()方法連接兩個集合 用 ==A.union(B)== 與前面說的.add()差別在於若是兩集合有重複物件 可以去除相同的物件只留下唯一值 ![](https://i.imgur.com/YNs9YUV.png) ```python= # Python code fruits = {'banana', 'orange', 'mango', 'lemon','tomato'} vegetables = {'tomato', 'potato', 'cabbage','onion', 'carrot'} print(fruits.union(vegetables)) ``` :::success {'orange', 'tomato', 'lemon', 'mango', 'potato', 'onion', 'banana', 'cabbage', 'carrot'} ::: R與Python一樣都是使用 ==union(A,B)== 函數 ```r= # R code fruits <- c('banana', 'orange', 'mango', 'lemon','tomato') vegetables <- c('tomato', 'potato', 'cabbage','onion', 'carrot') print(union(fruits,vegetables)) ``` :::success [1] "banana" "orange" "mango" "lemon" "tomato" "potato" "cabbage" "onion" "carrot" ::: ## Intersection交集 而若是想知道連集中重複的物件有哪些就要使用到 ==A.intersection(B)== ![](https://i.imgur.com/rJQr7aV.png) ```python= # Python code python = {'p', 'y', 't', 'h', 'o','n'} rstudio = {'r', 's', 't', 'u', 'd','i', 'o'} python.intersection(rstudio) ``` :::success {'t', 'o'} ::: 而R語言一樣也是使用 ==intersect(A,B)== 函數 ```r= # R code python <- c('p', 'y', 't', 'h', 'o','n') rstudio <- c('r', 's', 't', 'u', 'd','i', 'o') print(intersect(python,rstudio)) ``` :::info [1] "t" "o" ::: ## subset子集與superset超集 設A和B是兩個集合,如果A的任意一個元素都是B的元素 則稱A為B的子集(subset) 在Python中使用 ==A.issubset(B)== 函數判斷 若B為A的超(母)集(superset) 記為A⊆B,讀作B包含於A 使用 ==A.issuperset(B)== 進行判斷 ![](https://i.imgur.com/xAg6KTr.png) ```python= # Python code whole_numbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } even_numbers = { 0, 2, 4, 6, 8, 10 } whole_numbers.issubset(even_numbers) ``` :::success False ::: ```python=+ even_numbers.issubset(whole_numbers) ``` :::success True ::: ```python=+ whole_numbers.issuperset(even_numbers) ``` :::success True ::: 然而R似乎沒有這類的函數 但可以 %in% 運算子及all()函數達到與.issubset()相同的功能 ```r= # R code whole_numbers <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) even_numbers <- c(0, 2, 4, 6, 8, 10) even_numbers %in% whole_numbers ``` :::success [1] TRUE TRUE TRUE TRUE TRUE TRUE ::: ```r=+ all(whole_numbers %in% even_numbers) ``` 利用all()函數判斷是否全為TRUE 是的話回傳TRUE :::success [1] TRUE ::: ## Difference Between Two Sets比對兩集合差異 ![](https://i.imgur.com/sPISX8W.png) 若是使用Python要查看兩集合差異則使用 ==A.difference(B)== 函數 而R中可以使用 ==setdiff(A, B)== ```python= # Python code whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} even_numbers = {0, 2, 4, 6, 8, 10} whole_numbers.difference(even_numbers) ``` :::success {1, 3, 5, 7, 9} ::: ```r= # R code whole_numbers <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) even_numbers <- c(0, 2, 4, 6, 8, 10) setdiff(whole_numbers, even_numbers) ``` :::success [1] 1 3 5 7 9 ::: ### symmetric difference對稱差集 屬於A而不屬於B,或屬於B而不屬於A的元素所組成的集合稱為A與B的對稱差集 在Python則使用 ==A.symmetric_difference(B)== 函數求出AB的對稱差集 ![](https://i.imgur.com/6xyUo7f.png) ```python= # Python code whole_numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} even_numbers = {1, 2, 3, 4, 5} whole_numbers.symmetric_difference(even_numbers) ``` :::success {0, 6, 7, 8, 9, 10} ::: 然而在R似乎沒有對應的函數 但由下圖知道我們可以先將A、B做聯集後扣除其交集 也可以得到相同解答 ![Uploading file..._ql3ljd2ns]() ```r= # R code whole_numbers <- c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) even_numbers <- c(1, 2, 3, 4, 5) setdiff(union(whole_numbers, even_numbers),intersect(whole_numbers, even_numbers)) ``` :::success [1] 0 6 7 8 9 10 ::: 全文分享至 https://www.facebook.com/LHB0222/ 有疑問想討論的都歡迎於下方留言 喜歡的幫我分享給所有的朋友 \o/ 有所錯誤歡迎指教 # [:page_with_curl: 全部文章列表](https://hackmd.io/@LHB-0222/AllWritings) ![](https://i.imgur.com/47HlvGH.png)