Python學習筆記第三周

來源:互聯網
上載者:User

標籤:mod   call   關係   不同   ace   remove   mos   拷貝   color   

一、基礎概念

1、集合:

集合是一個無序的、不重複的資料租戶,它的作用:

1、去重複、把一個列表變成集合,就會自動去重

2、關係測試,測試兩組資料之間的交集、差集、並集等關係

  集合的方法:

    a、設定集合

>>> list_1 = [1,3,5,6,77,3,12]   #列表1>>> list_2 = [1,3,88,4,33,77]  #列表2>>> list_1 = set(list_1)  #通過set方法可以將列表變為集合>>> list_1{1, 3, 5, 6, 12, 77}  #集合list_1現在已經去重>>> list_2 = set(list_2)>>> list_2{1, 33, 3, 4, 77, 88}>>>print(list_1,type(list_1)) #列印list_1類型,發現為set類型,證明為集合

{1, 3, 5, 6, 12, 77} <class ‘set‘>

    

    b、取交集 intersection(list_1  list_2共有的元素)

>>> list_1.intersection(list_2)     {1, 3, 77}>>> print(list_1  & list_2){1, 3, 77}#上述兩種方法均可以取出list_1與list_2的交集

    c、取並集union(list_1 與list_2中全部去重元素)

>>> list_1.union(list_2)                  {1, 33, 3, 4, 5, 6, 12, 77, 88}>>> list_1  | list_2{1, 33, 3, 4, 5, 6, 12, 77, 88}#上述兩種方法取兩個集合的全部去重元素

    d、取差集(差集是一個集合有,但是另一個集合沒有的元素,所以差集可以得到兩種不同的結果)

    

>>> list_1.difference(list_2) #list_1中存在但是list_2中不存在{12, 5, 6}>>> list_2.difference(list_1) ##list_2中存在但是list_1中不存在{88, 33, 4}

>>> list_1 - list_2
{12, 5, 6}
>>> list_2 - list_1
{88, 33, 4}

    e、判斷子集(判斷一個集合是否在另一個集合中)

>>> list_3 = set([1,3])>>> list_3.issubset(list_2)True>>> list_3.issubset(list_1)True

    f、判斷父集

>>> list_1.issuperset(list_3)True

    g、對稱差集symmetri_difference(去掉list_1與list_2的交集後存入其他剩餘元素)

>>> list_1.symmetric_difference(list_2){33, 4, 5, 6, 88, 12}>>> list_1 ^ list_2{33, 4, 5, 6, 88, 12}

   

  基本操作:

    a、添加(集合操作沒有插入動作,只能添加)

>>> list_1{1, 3, 5, 6, 12, 77}>>> list_1.add(999)    #添加單項>>> list_1.add(‘wang‘)>>> list_1{1, 3, 5, 6, 999, 12, 77, ‘wang‘}>>> list_1.update([‘alex‘,‘john‘,‘tom‘])  #添加多項>>> list_1{1, 3, 5, 6, 999, 12, 77, ‘john‘, ‘tom‘, ‘wang‘, ‘alex‘}

    b、刪除

>>> list_1{1, 3, 5, 6, 999, 12, 77, ‘john‘, ‘tom‘, ‘wang‘, ‘alex‘}>>> list_1.remove(999)  #通過remove方法刪除,刪除後不顯示刪除項,刪除需要明確指定刪除項的內容,因為集合是無序的,沒有索引的概念>>> list_1{1, 3, 5, 6, 12, 77, ‘john‘, ‘tom‘, ‘wang‘, ‘alex‘}>>> list_1.remove(wang)  #針對字串需要加引號Traceback (most recent call last):  File "<stdin>", line 1, in <module>NameError: name ‘wang‘ is not defined>>> list_1.remove(‘wang‘)>>> list_1{1, 3, 5, 6, 12, 77, ‘john‘, ‘tom‘, ‘alex‘}>>> list_1.pop()  #pop方法屬於隨機刪除,刪除後顯示刪除的內容1>>> list_1.pop()3>>> list_1.pop()5>>> list_1.pop()6>>> list_1.pop()12>>> list_1.pop()77>>> list_1.pop()‘john‘>>> list_1{‘tom‘, ‘alex‘}

    c、discard刪除

>>> list_1{‘wang‘, 1, 3, 5, 6, 999, 12, 77, ‘john‘, ‘tom‘, ‘alex‘}>>> list_1.discard(999)   >>> list_1{‘wang‘, 1, 3, 5, 6, 12, 77, ‘john‘, ‘tom‘, ‘alex‘}>>> list_1.discard(888)    #discard與remove的不同之處在於,discard如果是刪除不存在的內容,不會報錯,但是remove會報錯>>> list_1.remove(999)Traceback (most recent call last):  File "<stdin>", line 1, in <module>KeyError: 999

    d、長度len

>>> len(list_1)10>>> list_1{‘wang‘, 1, 3, 5, 6, 12, 77, ‘john‘, ‘tom‘, ‘alex‘}

    e、成員判斷(in 和not in)

>>> list_1{‘wang‘, 1, 3, 5, 6, 12, 77, ‘john‘, ‘tom‘, ‘alex‘}>>> ‘wang‘ in list_1True>>> 11 in list_1      False>>> 11 not in list_1True

    f、拷貝(和list一樣,都屬於淺拷貝)

 

Python學習筆記第三周

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.