1. 程式人生 > >set的特性和基本用法——python3.6

set的特性和基本用法——python3.6

技術 card pda 測試 display pla 不能 差集 pytho

  • 特性
    • 無序,不重復的數據組合,用{}表示,eg:{1,2,3,4,5,6}
  • 用途
    • 去重,把一個列表變成集合,就自動去重了
    • 關系測試,測試兩組數據之間的交集,差集,並集,對稱差集,包含(子集和超集,相交和不相交)關系
  • 基本語法
    • set():創建一個空集合,用set()創建,不能直接用s = {},這是創建一個空字典
    • 技術分享
      s = set()
      print(type(s))    # =><class set>
      View Code
    • add:往集合中添加一個元素,如果添加的元素集合中已有,則集合沒有任何變化,只能添加不可變數據
    • 技術分享
      s = {1,2,3,4
      ,5} s.add(1) print(s) # =>{1, 2, 3, 4, 5} s.add(6) print(s) # =>{1, 2, 3, 4, 5, 6}
      View Code
    • update:往集合中添加多個元素,自動去重
    • 技術分享
      s = {1,2,3,4,5}
      s.update({1,2,6,7,8})
      print(s)    # =>{1, 2, 3, 4, 5, 6, 7, 8}
      View Code
    • discard:刪除指定元素,如果要刪除的元素集合中不存在,do nothing
    • 技術分享
      s = {1,2,3,4,5}
      s.discard(1)
      print(s)    # 
      =>{2, 3, 4, 5}
      View Code
    • remove:刪除指定元素,如果要刪除的元素集合中不存在,報錯
    • 技術分享
      s = {1,2,3,4,5}
      s.remove(1)
      print(s)    # =>{2, 3, 4, 5}
      s.remove(6) # =>報錯
      View Code
    • pop:隨機刪除一個元素,無序的,當集合為空時,報錯
    • 技術分享
      s = {1,2}
      s.pop()
      print(s)    # =>{2}
      s.pop()
      s.pop()     # =>報錯
      View Code
    • clear:清空
    • 技術分享
      s = {1,2,3,4,5}
      s.clear()
      print(s)    # 
      =>set()
      View Code
    • copy:淺復制
    • 技術分享
      s = {1,2,3,4,5}
      s1 = s.copy()
      print(s1)   # =>{1,2, 3, 4, 5}
      View Code
    • difference:差集,差集後的結果是一個新的集合

    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.difference(s2)
      print(s3)   # =>{1, 2, 3}
      s3 = s1-s2
      print(s3)   # =>{1, 2, 3}
      s4 = s2.difference(s1)
      print(s4)   # =>{8, 6, 7}
      s4 = s2-s1
      print(s4)   # =>{8, 6, 7}
      View Code
    • difference_update:刪除s1集合中含有s2集合中的元素,並返回給s1

    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.difference_update(s2)
      print(s1)   # =>{1, 2, 3}
      View Code
    • intersection:交集,交集後的結果是一個新的集合

    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.intersection(s2)
      print(s3)   # =>{4, 5}
      s3 = s1 & s2
      print(s3)   # =>{4, 5}
      s3 = s2.intersection(s1)
      print(s3)   # =>{4, 5}
      s3 = s2 & s1
      print(s3)   # =>{4, 5}
      View Code
    • intersection_update:將s1和s2的交集更新給s1

    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s1.intersection_update(s2)
      print(s1)   # =>{4, 5}
      View Code
    • union:並集

    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.union(s2)
      print(s3)   # =>{1, 2, 3, 4, 5, 6, 7, 8}
      s3 = s1 | s2
      print(s3)   # =>{1, 2, 3, 4, 5, 6, 7, 8}
      View Code
    • isdisjoint:判斷兩個集合是否有交集,如果沒有,返回True,如果有,返回False

    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = {8,9}
      print(s1.isdisjoint(s2))   # => False
      print(s1.isdisjoint(s3))   # =>True
      View Code
    • issubset:判斷s2是否是s1的子集,如果是,則True,or,False

      技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5}
      print(s2.issubset(s1))   # =>True
      View Code
    • issupper:判斷s1是否是s2的超集
    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5}
      print(s1.issuperset(s2))   # =>True
      View Code
    • symmetric_difference:對稱差集
    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s3 = s1.symmetric_difference(s2)
      print(s3)   # =>{1, 2, 3, 6, 7, 8}
      s3 = s2.symmetric_difference(s1)
      print(s3)   # =>{1, 2, 3, 6, 7, 8}
      View Code
    • symmetric_difference_updata:將對稱差集更新給s1,或者s2
    • 技術分享
      s1 = {1,2,3,4,5}
      s2 = {4,5,6,7,8}
      s1.symmetric_difference_update(s2)
      print(s1)   # =>{1, 2, 3, 6, 7, 8}
      s2.symmetric_difference_update(s1)
      print(s2)   # =>{1, 2, 3, 4, 5}
      View Code

  • 其他用法:
    • in ,not in 判斷某元素是否在集合內
    • == ,!=判斷兩個集合是否相等

set的特性和基本用法——python3.6