集合的增刪改查、集合類型的關係測試

                                               集合類型

  • 如何同時找出買了IPhone7和8的人?

 

 

 

  • 集合的增刪改查

 1 #建立集合
 2 s = {}                        #字典
 3 print(type(s))                #結果爲:<class 'dict'>
 4 
 5 s1 = {1,2,3,4,5}              #集合
 6 print(type(s1))               #結果爲:<class 'set'>
 7 
 8 #將列表轉化爲集合
 9 l= [1,2,3,4,5,2,3] 10 print(l)                      #結果爲:[1, 2, 3, 4, 5, 2, 3]
11 print(set(l))                 #結果爲:{1, 2, 3, 4, 5}
12 print(type(set(l)))           #結果爲:<class 'set'>
13 
14 
15 #集合的增刪改查
16 #增長
17 s2 = {1,2,3,4,5} 18 #增長一個已存在的值
19 s2.add(2) 20 print(s2)                    #結果爲:{1, 2, 3, 4, 5}
21 #增長一個沒有的值
22 s2.add(8) 23 print(s2)                    #結果爲:{1, 2, 3, 4, 5, 8}
24 
25 #刪除
26 # 集合是無序的,隨機刪除一個元素
27 s2.pop() 28 print(s2)                    #結果爲:{2, 3, 4, 5, 8}
29 #刪除集合中指定的元素
30 s2.remove(8) 31 print(s2)                    #結果爲:{2, 3, 4, 5}
32 #remove方法,刪除集合中不存在的元素會報錯
33 #s2.remove(6) #結果爲:KeyError: 6,
34 #discard方法,刪除集合中不存在的元素不會報錯(相似於字典中的get方法)
35 s2.discard(6) 36 print(s2)                    #結果爲:{2, 3, 4, 5}
37 
38 #把多個值加入集合
39 s2.update([10,12,15,1,2,3,4,5]) 40 print(s2)                    #結果爲:{1, 2, 3, 4, 5, 10, 12, 15}
41 
42 #清空set中的內容;
43 s2.clear() 44 print(s2)                    #結果爲:set()
45 
46 #print(help(dic4.pop()))查看字典的幫助信息;

 

 

                                        集合類型的關係測試

 1 #交集(買了iphone7而且還買了iPhone8的人)
 2 iphone7 = {'alex','rain','jack','old_driver'}  3 iphone8 = {'alex','jack','shanshan','old_boy'}  4 print(iphone7.intersection(iphone8))              #結果爲:{'jack', 'alex'}
 5 print(iphone7 & iphone8)                          #結果爲:{'jack', 'alex'}
 6 
 7 #差集
 8 iphone7 = {'alex','rain','jack','old_driver'}  9 iphone8 = {'alex','jack','shanshan','old_boy'} 10 #買了iphone7卻沒有買iPhone8的人
11 print(iphone7.difference(iphone8))                #結果爲:{'rain', 'old_driver'}
12 print(iphone7 - iphone8)                          #結果爲:{'rain', 'old_driver'}
13 #買了iphone8卻沒有買iPhone7的人
14 print(iphone8.difference(iphone7))                #結果爲:{'shanshan', 'old_boy'}
15 print(iphone8 - iphone7)                          #結果爲:{'shanshan', 'old_boy'}
16 
17 #並集(買了iphone7或者iPhone8的人)
18 iphone7 = {'alex','rain','jack','old_driver'} 19 iphone8 = {'alex','jack','shanshan','old_boy'} 20 print(iphone8.union(iphone7))                     #結果爲:{'shanshan', 'old_boy', 'rain', 'alex', 'jack', 'old_driver'}
21 print(iphone8 | iphone7)                          #結果爲:{'shanshan', 'old_boy', 'rain', 'alex', 'jack', 'old_driver'}
22 print(iphone7.union(iphone8))                     #結果爲:{'old_driver', 'old_boy', 'alex', 'rain', 'shanshan', 'jack'}
23 print(iphone7 | iphone8)                          #結果爲:{'old_driver', 'old_boy', 'alex', 'rain', 'shanshan', 'jack'}
24 
25 #對稱差集(只買了iphone7 或者 只買了iPhone8的人)
26 iphone7 = {'alex','rain','jack','old_driver'} 27 iphone8 = {'alex','jack','shanshan','old_boy'} 28 print(iphone7.symmetric_difference(iphone8))       #結果爲:{'old_driver', 'rain', 'shanshan', 'old_boy'}
29 print(iphone8.symmetric_difference(iphone7))       #結果爲:{'old_driver', 'rain', 'shanshan', 'old_boy'}
30 print(iphone7 ^ iphone8)                           #結果爲:{'old_driver', 'rain', 'shanshan', 'old_boy'}
31 print(iphone8 ^ iphone7)                           #結果爲:{'old_driver', 'rain', 'shanshan', 'old_boy'}
32 
33 #子集和超集(超集和子集能夠使用> 或<符號來表示)
34 s = {1,2,3,4} 35 s2 = {2,3,5,6} 36 s2.add(1) 37 s2.add(4) 38 print(s2)                                          #結果爲:{1, 2, 3, 4, 5, 6}
39 print(s)                                           #結果爲:{1, 2, 3, 4}
40 #判斷 s 是否是 s2 的子集 s <= s2
41 print(s.issubset(s2))                              #結果爲:True
42 #判斷 s 是否是 s2 的超集 s >= s2
43 print(s.issuperset(s2))                            #結果爲:False
44 #判斷 s2 是否是 ss 的超集 s2 >= s
45 print(s2.issubset(s2))                             #結果爲:True
46 
47 #當集合中的數據量特別大時,能夠用s.isdisjoint()判斷 是否是 不相交
48 print(s.isdisjoint(s2))                            #結果爲:False
49 print(s2.isdisjoint(s))                            #結果爲:False
50 
51 l = {1,2,3,4,-1,-2} 52 l1 = {1,2,3,4,5,6} 53 #將l與l1差集的結果賦值給l
54 l.difference_update(l1) 55 print(l)                                           #結果爲:{-2, -1}
56 #將l與l1差集的結果賦值給l1
57 l1.difference_update(l) 58 print(l1)                                          #結果爲:{5, 6}
相關文章
相關標籤/搜索