python基礎知識(集合)

來源:互聯網
上載者:User

標籤:python 集合set()、frozenset()工廠函數

    在python中集合set是一種基本資料的類型,它有可變集合(set())和不可變集合(frozenset)兩種。建立集合set集合set添加集合刪除交集並集差集的操作都是非常實用的方法,接下來將介紹結合的相關知識。

一、集合特點

1、類似字典dict,但是只有key卻沒有value值;

2、集合的儲存沒有固定的順序

3、由於集合裡面的元素不能重複所以集合一般用來去重

二、集合的定義

常見的有兩種方法

1、直接定義類似字典但是沒有value值,例如set1={"openatck","and","list","dict","set"}2、建立一個set,提供一個list作為輸入集合set2=set(["openatck","and","list","dict","set"])兩個的輸出結果{‘openatck‘, ‘list‘, ‘and‘, ‘set‘, ‘dict‘} {‘openatck‘, ‘list‘, ‘and‘, ‘set‘, ‘dict‘}

三、集合元素的操作

 1、 增加元素可以使用add()方法增加元素

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openatck","and","list","dict","set"}set2=set(["openatck","and","list","dict","set"])set2.add("winnerlook")print (set1,set2)結果:{‘set‘, ‘openatck‘, ‘and‘, ‘dict‘, ‘list‘} {‘openatck‘, ‘winnerlook‘, ‘list‘, ‘and‘, ‘dict‘, ‘set‘}

2、集合元素的增加update()方法

    update()是把要傳入的元素拆分,做為個體傳入到集合中

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openatck","and","list","dict","set"}set2=set(["openatck","and","list","dict","set","winnerlook"])set2.update("winnerlook","MYSQlDBA")print (set2)結果:  {‘B‘, ‘M‘, ‘r‘, ‘S‘, ‘Y‘, ‘e‘, ‘winnerlook‘, ‘set‘, ‘i‘, ‘Q‘, ‘  and‘, ‘o‘, ‘w‘, ‘n‘, ‘dict‘, ‘k‘,t}  這時我們發現將我們更新傳入的值全部按每個字元當做一個元素插入到當前的集合裡面了

3、集合的刪除

   在前面列表的學習裡面我們學習了clear()、pop()、remove()、del等方法可以刪除列表中的元素,接下來我們分析一下集合刪元素的方法。


clear()方法

 clear()方法還是和列表中的一樣直接會清空集合中的所有元素,例如:

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openatck","and","list","dict","set"}set2=set(["openatck","and","list","dict","set","winnerlook"])#set2.update("winnerlook","MYSQlDBA")set2.clear()print (set1,set2)結果:{‘set‘, ‘dict‘, ‘openatck‘, ‘list‘, ‘and‘} set()返回結果和是一個空的集合,而列表使用clear()方法後是返回空的列表

remove()方法

 remove()方法是移除集合中的某個元素,但是元素必須存在,不然會報錯

#!/usr/bin/env python#_*_coding:utf-8_*_list=["openatck","and","list","dict","set"]set1={"openatck","and","list","dict","set"}set2=set(["openatck","and","list","dict","set","winnerlook"])#set2.update("winnerlook","MYSQlDBA")set2.remove("winnerlook")list.remove("and")print (list,set2)結果:[‘openatck‘, ‘list‘, ‘dict‘, ‘set‘] {‘openatck‘, ‘and‘, ‘dict‘, ‘list‘, ‘set‘}

pop()方法

 集合的pop()方法和列表的pop()方法是不一樣的,列表的可以帶下標的去刪除,列表是預設刪除最後一個元素,由於集合的儲存是沒有固定順序的所以刪除的元素時隨機的。我們看一下下面的樣本,相同的程式多次執行結果卻是不一致。

#!/usr/bin/env python#_*_coding:utf-8_*_list=["openatck","and","list","dict","set"]set1={"openatck","and","list","dict","set"}set2=set(["openatck","and","list","dict","set","winnerlook"])#set2.update("winnerlook","MYSQlDBA")set2.pop()list.pop()print (list,set2)結果1:[‘openatck‘, ‘and‘, ‘list‘, ‘dict‘] {‘dict‘, ‘winnerlook‘, ‘and‘, ‘set‘, ‘list‘}#刪除openstack結果2:[‘openatck‘, ‘and‘, ‘list‘, ‘dict‘] {‘list‘, ‘set‘, ‘and‘, ‘openatck‘, ‘dict‘} #刪除winnerlook結果3:[‘openatck‘, ‘and‘, ‘list‘, ‘dict‘] {‘list‘, ‘winnerlook‘, ‘openatck‘, ‘set‘, ‘and‘}#刪除dict結果4:[‘openatck‘, ‘and‘, ‘list‘, ‘dict‘] {‘openatck‘, ‘dict‘, ‘winnerlook‘, ‘and‘, ‘list‘}#刪除set這裡為什麼是隨機刪除的,個人猜想是因為集合裡面儲存順序不是固定的原因吧,這樣就會出現隨機刪除的現象。

del 刪除

    在刪除某個定義的集合時,我們還可以採用del方法,這樣就可以將一個集合刪除,但是這種刪除會報沒有定義這個集合的錯。

#!/usr/bin/env python#_*_coding:utf-8_*_list=["openatck","and","list","dict","set"]set1={"openatck","and","list","dict","set"}set2=set(["openatck","and","list","dict","set","winnerlook"])#set2.update("winnerlook","MYSQlDBA")del set2list.pop()print (list,set2)結果:    print (list,set2)NameError: name ‘set2‘ is not defined

4、集合的訪問

    由於集合本身是無序的,所以不能為集合建立索引或切片操作,只能迴圈遍曆或使用in、not in來訪問或判斷集合元素。

5、元素與集合、集合與集合的關係

  在我們高中學習集合相關的知識時,我們就學習到集合的交際、並集、補集等概念,而這部分概念在其python編程的資料類型集合裡面還是適用的。

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M00/8F/37/wKiom1jX2uLAR_lvAAHJEC75CFw968.png-wh_500x0-wm_3-wmp_4-s_278262170.png" title="jihe.png" alt="wKiom1jX2uLAR_lvAAHJEC75CFw968.png-wh_50" />

下面簡單的示範以下上面的集合關係和相關的操作:

元素與集合的關係:

 in  一般是用來表示某個元素是否在某個集合裡面,傳回值為Ture和False

#!/usr/bin/env python#_*_coding:utf-8_*_list=["openatck","and","list","dict","set"]set1={"openatck","and","list","dict","set"}set2=set(["openstack","and","list","dict","set","winnerlook"])print ("openstack" in set2)print ("docker" in set2)結果:TrueFalse由於set2中包含openstack元素但是不包含docker元素,所以判斷一個返回Ture,一個返回False

not in 其實和in表達的意思差不多

#!/usr/bin/env python#_*_coding:utf-8_*_list=["openatck","and","list","dict","set"]set1={"openatck","and","list","dict","set"}set2=set(["openstack","and","list","dict","set","winnerlook"])print ("openstack"  not in set2)print ("docker"  not  in set2)結果:FalseTrue

== 等於關係

#!/usr/bin/env python#_*_coding:utf-8_*_set2=set(["openstack","and","list","dict","set","winnerlook"])set3={"openstack","and","list","dict","set","winnerlook"}print(set2==set3)結果:True  表示兩個集合相等set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","and","list","dict","set","winnerlook"])set3={"openstack",17,"list","dict","set","winnerlook"}print(set1==set3)結果:True  表示兩個集合相等 運算式和結果一樣

!= 不等於關係 表示兩個集合的元素不一樣

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","and","list","dict","set","winnerlook"])set3={"openstack",17,"list","dict","set","winnerlook"}print(set1!=set2)print(set1!=set3)結果:TrueFalse

集合與集合的關係

子集(<)與真子集(<=)

子集表示一個集合裡面的元素被另一個集合包含,而真子集是說的一個集合的元素恰好和另一個集合相等。

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","and","list","dict","set","winnerlook"])set3={"openstack",17,"list","dict","set","winnerlook"}set4={"openstack","list","dict","set","winnerlook"}print(set1<=set3)print(set4< set1)print(set4<=set3)結果:TrueTrueTrue

嚴格超集(>)和非嚴格超集(>=)

  這裡的關係和上面的字跡關係是一樣的,只不過是用的表示相反的關係

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","and","list","dict","set","winnerlook"])set3={"openstack",17,"list","dict","set","winnerlook"}set4={"openstack","list","dict","set","winnerlook"}print(set1>=set3)print(set4>set1)print(set4>=set3)結果:TrueFalseFalse

交集(&)或者用intersection()

表示兩個集合中相同的元素

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","list","dict","winnerlook",99,100,0.99])#set3={"openstack",17,"list","dict","set","winnerlook"}#set4={"openstack","list","dict","set","winnerlook"}print(set1&set2)print(set1.intersection(set2))結果:{‘openstack‘, ‘list‘, ‘dict‘, ‘winnerlook‘}{‘openstack‘, ‘list‘, ‘dict‘, ‘winnerlook‘}

並集 (|)或者用union()表示

表示兩個集合中一共包含的元素

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","list","dict","winnerlook",99,100,0.99])#set3={"openstack",17,"list","dict","set","winnerlook"}#set4={"openstack","list","dict","set","winnerlook"}#print(set1&set2)#print(set1.intersection(set2))print(set1|set2)print(set1.union(set2))結果:{0.99, 99, 100, ‘dict‘, ‘set‘, 17, ‘winnerlook‘, ‘list‘, ‘openstack‘}{0.99, 99, 100, ‘dict‘, ‘set‘, 17, ‘winnerlook‘, ‘list‘, ‘openstack‘}

差補(-)或者difference()

相對補集

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","list","dict","winnerlook",99,100,0.99])#set3={"openstack",17,"list","dict","set","winnerlook"}#set4={"openstack","list","dict","set","winnerlook"}#print(set1&set2)#print(set1.intersection(set2))print(set1-set2)print(set1.difference(set2))結果:{17, ‘set‘}{17, ‘set‘}set2相對sst1的補集即set2中有,但是set1中是沒有的

對稱差分(^)或者symmetric_difference()

對稱差分是集合的XOR(‘異或’),取得的元素屬於set1,set2但不同時屬於set1和set2

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","list","dict","winnerlook",99,100,0.99])#set3={"openstack",17,"list","dict","set","winnerlook"}#set4={"openstack","list","dict","set","winnerlook"}#print(set1&set2)#print(set1.intersection(set2))print(set1^set2)print(set1.symmetric_difference(set2))結果:{0.99, 17, 99, 100, ‘set‘}{0.99, 17, 99, 100, ‘set‘}

6、集合之間的and與or關係

注意and關係是取值set2     or關係是取值set1

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","list","dict","winnerlook",99,100,0.99])#set3={"openstack",17,"list","dict","set","winnerlook"}#set4={"openstack","list","dict","set","winnerlook"}#print(set1&set2)#print(set1.intersection(set2))print(set1 and set2)print(set1 or set2){0.99, 99, 100, ‘dict‘, ‘winnerlook‘, ‘list‘, ‘openstack‘}{‘dict‘, ‘set‘, ‘winnerlook‘, 17, ‘list‘, ‘openstack‘}

7、集合、列表、元組、字串之間轉換

我們可以將集合根據不同的資料類型轉換成其他的格式

list(set1)將集合轉換成列表

tuple(set1)將集合轉換成元組

str(set1) 將集合轉換成字串

#!/usr/bin/env python#_*_coding:utf-8_*_set1={"openstack",9+8,"list","dict","set","winnerlook"}set2=set(["openstack","list","dict","winnerlook",99,100,0.99])#set3={"openstack",17,"list","dict","set","winnerlook"}#set4={"openstack","list","dict","set","winnerlook"}#print(set1&set2)#print(set1.intersection(set2))print(str(set1),type(str(set1)))print(list(set1),type(list(set1)))print(tuple(set1),type(tuple(set1)))結果:{‘set‘, ‘list‘, ‘winnerlook‘, ‘dict‘, 17, ‘openstack‘} <class ‘str‘>[‘set‘, ‘list‘, ‘winnerlook‘, ‘dict‘, 17, ‘openstack‘] <class ‘list‘>(‘set‘, ‘list‘, ‘winnerlook‘, ‘dict‘, 17, ‘openstack‘) <class ‘tuple‘>

8、集合的簡單應用

由於集合的元素不能重複,所以可以用來去重

list2=[2,-3,-8,0,100,5005,737737,10010,-3,-8,0,100,10010]set222=set(list2)print(set222)結果:{0, 2, 100, 737737, 5005, -8, 10010, -3}

四、集合的內建函數與內建方法

1、len():返回集合元素個數

2、set()、frozenset()工廠函數

3、所有集合方法:

650) this.width=650;" src="https://s4.51cto.com/wyfs02/M02/8F/39/wKiom1jX67SQM9Y8AAHncmPnLjo334.png-wh_500x0-wm_3-wmp_4-s_2366317023.png" title="jihe2.png" alt="wKiom1jX67SQM9Y8AAHncmPnLjo334.png-wh_50" />

4、僅適合可變集合的

    

650) this.width=650;" src="https://s5.51cto.com/wyfs02/M02/8F/36/wKioL1jX6_bSV6iOAAHBQYpAyW4283.png-wh_500x0-wm_3-wmp_4-s_4150361123.png" title="jieh3.png" alt="wKioL1jX6_bSV6iOAAHBQYpAyW4283.png-wh_50" />

650) this.width=650;" src="https://s3.51cto.com/wyfs02/M00/8F/36/wKioL1jX6_ezBeapAAF4tjRwMjY858.png-wh_500x0-wm_3-wmp_4-s_1527912733.png" title="jihe4.png" alt="wKioL1jX6_ezBeapAAF4tjRwMjY858.png-wh_50" />

參考文檔:http://www.cnblogs.com/BeginMan/p/3160565.html

本文出自 “堅持夢想” 部落格,請務必保留此出處http://dreamlinux.blog.51cto.com/9079323/1910566

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.