iT邦幫忙

2025 iThome 鐵人賽

DAY 13
0

什麼是集合?

  • 集合是一組不重複的元素,沒有順序
  • 適合用來做去重複、判斷某個元素是否存在、集合運算(交集、聯集)
  • Python用大括號 {} 或 set() 來建立集合

1. 建立集合
https://ithelp.ithome.com.tw/upload/images/20250927/20178872SHodPbXvBe.png
輸出順序可能不同,因為集合不保證順序
💡 集合是「無序」的,所以每次執行,元素排列順序都可能會不同
https://ithelp.ithome.com.tw/upload/images/20250927/20178872sn4gTJmWoY.png
如果要建立空集合,要用 set() 而不是 {}(因為 {} 會變成空字典)
https://ithelp.ithome.com.tw/upload/images/20250927/20178872GGyoDjKCTM.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20250927/20178872AopgfqPdRc.png

  • set() 是建立一個空集合的方法
  • 為什麼不用 {} 呢?因為 {} 在 Python 裡預設是空字典,不是集合
  • 但是當大括號裡面有東西時,Python 會把它當作集合 set,而空 {} 則會變成字典
  • 所以要建立空集合,就要用 set()

2. 集合的基本操作
(1) 新增元素
https://ithelp.ithome.com.tw/upload/images/20250927/201788722JfikJgODR.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20250927/20178872T7aDvrFtHC.png

  • add() 是集合(set)的新增方法
  • 作用:把 "durian" 這個元素加入 fruits 集合
  • 如果 "durian" 已經在集合裡了,就不會重複新增(因為集合不會存重複值)
  • 但因為集合是無序的,所以輸出結果的順序可能不是按照原本加入的順序

(2) 刪除元素
https://ithelp.ithome.com.tw/upload/images/20250927/20178872QT2Yv0y4fF.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20250927/20178872fxnWmSGbxJ.png

  • remove() 用來刪除集合中指定的元素,如果 "banana" 不存在,會報錯

https://ithelp.ithome.com.tw/upload/images/20250927/20178872b6O7PTSTfS.png
https://ithelp.ithome.com.tw/upload/images/20250927/20178872JE0mT7iQGR.png

  • discard() 也是用來刪除指定的元素,如果元素不存在,不會報錯,什麼都不做

https://ithelp.ithome.com.tw/upload/images/20250927/20178872dlaVCoUaxQ.png
https://ithelp.ithome.com.tw/upload/images/20250927/20178872ONakay4b6g.png

(3) 判斷元素是否存在
https://ithelp.ithome.com.tw/upload/images/20250927/201788724j9PTmuNEP.png
https://ithelp.ithome.com.tw/upload/images/20250927/20178872olVwZjf9No.png

  • in 用來檢查元素是否存在於集合 (或其他容器)
  • 回傳結果會是布林值 (True / False)

3. 集合運算
集合最強大的地方就是它的數學運算
現在有A、B兩個集合:
https://ithelp.ithome.com.tw/upload/images/20250927/20178872idFrCqTmVD.png
(1) A | B → 聯集
把兩個集合的所有元素合併,去掉重複的元素
https://ithelp.ithome.com.tw/upload/images/20250927/20178872I1FO0T3KDZ.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20250927/20178872Y3A9G1IH5V.png
(2) A & B → 交集
取出兩個集合共有的元素
https://ithelp.ithome.com.tw/upload/images/20250927/20178872uAa2MaA4gh.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20250927/20178872x7FevVzPgS.png
(3) A - B → 差集
取出 A 裡有、但 B 裡沒有的元素
https://ithelp.ithome.com.tw/upload/images/20250927/20178872BwAXx6MI7i.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20250927/201788725KCVEBJp5d.png
(4) A ^ B → 對稱差
取出只在其中一個集合出現的元素
https://ithelp.ithome.com.tw/upload/images/20250927/20178872QoQJnKzM98.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20250927/201788724QiWUl3Vke.png

4. 集合應用範例:去除重複值
https://ithelp.ithome.com.tw/upload/images/20250927/20178872KZ7mq4jG4p.png
結果會輸出
https://ithelp.ithome.com.tw/upload/images/20250927/2017887206fIK4bIsq.png

  • 這是一個列表 (list),裡面有重複的數字
  • set() 會把 numbers 轉換成一個集合 (set)
  • 轉換後得到的不是列表,而是集合,輸出用大括號 {}
  • 如果想再變回列表,可以使用 list(unique_numbers)

上一篇
Day12 : 字典 dict ─ key-value 存取、基本操作
系列文
學會 Python 不可怕:我每天學一點的 30 天筆記13
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言