您好,登錄后才能下訂單哦!
Python中如何操作集合?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
>>> name_1 = [1,2,3,4,7,8,7,10] #把列表轉換為集合 >>> name_1 = set(name_1) #轉換后,去重 >>> print(name_1,type(name_1)) {1, 2, 3, 4, 7, 8, 10} <class 'set'>
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #輸出結果 >>> name_1.intersection(name_2) {8, 1, 10, 3}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #輸出結果 >>> name_1.union(name_2) {1, 2, 3, 4, 5, 7, 8, 10}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #輸出結果 >>> name_1.difference(name_2) {2, 4, 7}
判斷一個集合是否是另一個集合的子集
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #輸出結果 >>> name_3.issubset(name_1) True
判斷一個集合是否是另一個集合的父集
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #輸出結果 >>> name_1.issuperset(name_3) True
把兩個集合沒有交集的數值取出來
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #輸出結果 >>> name_1.symmetric_difference(name_2) {2, 4, 5, 7}
判斷兩個集合是否有交集,沒有交集,則返回True
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_3 = [11] >>> name_1 = set(name_1) >>> name_2 = set(name_2) >>> name_3 = set(name_3) #有交集 >>> name_1.isdisjoint(name_2) False #無交集 >>> name_1.isdisjoint(name_3) True
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #結果輸出 >>> name_1 & name_2 {8, 1, 10, 3}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #結果輸出 >>> name_1 | name_2 {1, 2, 3, 4, 5, 7, 8, 10}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #結果輸出 >>> name_1 - name_2 {2, 4, 7}
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_2 = [1,3,5,8,10] >>> name_1 = set(name_1) >>> name_2 = set(name_2) #輸出 >>> name_1 ^ name_2 {2, 4, 5, 7}
>>> name_1 = [1,2,3,4,7,8,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #輸出 >>> name_3 <= name_1 True
>>> name_1 = [1,2,3,4,7,8,10] >>> name_3 = [1,2,3,4] >>> name_1 = set(name_1) >>> name_3 = set(name_3) #輸出 >>> name_1 >= name_3 True
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) #添加已存在,不報錯 >>> name_2.add(1) >>> name_2 {8, 1, 10, 3, 5} #添加不存在,添加一個新的數值 >>> name_2.add(11) >>> name_2 {1, 3, 5, 8, 10, 11}
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2.update([12,13,14]) #輸出結果 >>> name_2 {1, 3, 5, 8, 10, 12, 13, 14}
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2 {8, 1, 10, 3, 5} >>> name_2.remove(1) #輸出 >>> name_2 {8, 10, 3, 5} #刪除不存在的元素,會報錯 >>> name_2.remove(1) Traceback (most recent call last): File "<input>", line 1, in <module> KeyError: 1
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2 {8, 1, 10, 3, 5} #輸出 >>> name_2.pop() 8
>>> name_2 = [1,3,5,8,10] >>> name_2 = set(name_2) >>> name_2.discard(10) #輸出結果 >>> name_2 {8, 1, 3, 5} #刪除不存在元素,不報錯 >>> name_2.discard(10)
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #結果輸出 >>> len(name_1) 7
測試 x 是否是 s 的成員
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #結果輸出 >>> 1 in name_1 True
測試 x 是否不是 s 的成員
>>> name_1 = [1,2,3,4,7,8,7,10] >>> name_1 = set(name_1) #輸出 >>> 12 not in name_1 True
1、云計算,典型應用OpenStack。2、WEB前端開發,眾多大型網站均為Python開發。3.人工智能應用,基于大數據分析和深度學習而發展出來的人工智能本質上已經無法離開python。4、系統運維工程項目,自動化運維的標配就是python+Django/flask。5、金融理財分析,量化交易,金融分析。6、大數據分析。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。