Python之路【第四篇】python基礎 之基礎資料型別 (Elementary Data Type)之集合

來源:互聯網
上載者:User

標籤:intersect   子序列   discard   code   style   指定   返回   super   sdi   

基礎資料型別 (Elementary Data Type)之集合                                                                      

set

set集合,是一個無序且不重複的元素集合

 1 # set  不允許重複的集合 set允許重複的列表但是集合是無序的 2 #例如 3 # s = {1,23,23,4,55,55} 4 # print(s)    # 結果 {1, 4, 23, 55} 5  6 # 1、建立 7 # s = set() 8 # s = {11,22,33,44} 9 10 # 2、轉換11 # l = list()12 # s = set() #建立集合13 # s = set([11,22,33,11,22]) # 把列錶轉化成集合14 15 #3、 集合的內建方法16 17 # 1  add z在集合內添加元素,不要添加集合中有的 只能添加一個元素18 # se = {1,2,3,3}19 # se.add(44)20 # print(se)21 22 # 2  clear 清理集合23 # s1 = {11,22,33,44,55}24 # s1.clear()25 # print(s1)26 27 # 3 difference  - 號代表取差集28 # s1 = {11,22,33,44,55}29 # s2 = {22,55,66}30 # ret1 = s1.difference(s2)#找s1中存在,s2中不存在的集合   #s1-s231 # ret2 = s2.difference(s1)#找s2中存在,s1中不存在的集合 #s2-s132 # print(ret1)33 # print(ret2)34 35 # 4 difference_update36 # s1 = {11,22,33,44,55}37 # s2 = {22,55,66}38 # ret = s1.difference_update(s2)39 # # 找s1中存在,s2中不存在的集合,更新自己40 # print(s1)41 # print(ret)42 43 # 5 discard 移除指定元素 沒有不報錯44 # s1 = {11,22,33,44,55}45 # s1.discard(44)46 # s1.remove(11) #移除指定元素,沒有報錯47 # print(s1)48 49 # 6  intersection   求交集  &50 # se = {11,22,33}51 # be = {22,95,"suibian"}52 # # ret = se.intersection(be)53 # # se.intersection_update()54 # # print(ret)55 # # print(se)56 # ret = se.isdisjoint(be) #判斷來個是否由交集  有的話False 沒有True57 # print(ret)58 59 # 7  issubset 子序列60 # se = {11,22,33,44}61 # be = {11,22}62 # ret = se.issubset(be)63 #64 # ret1 = se.issuperset(be)  #父序列65 # print(ret)66 # print(ret1)67 68 # 8   #隨機刪除一個值 並傳回值69 # se = {11,22,33,44}70 # ret = se.pop()71 # print(ret)72 73 # 9  symmetric_difference ^ # 交差補集   #去除兩個集合相同部分,剩下不同的元素74 # se = {11,22,33,44}75 # be = {11,22,55,77}76 # ret = se.symmetric_difference(be)77 # print(ret)78 #79 # # 10  union  | # 交集  # 求兩個集合的公用部分80 # ret = se.union(be)81 # print(ret)82 83 # 10   update  更新列表84 # se = {11,22,33,44}85 # se.update([44,55])86 # print(se)

 

Python之路【第四篇】python基礎 之基礎資料型別 (Elementary Data Type)之集合

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.