set()建立的集合都是可以原處修改的集合,或者說可變的,也可以說是unhashable。
創(chuàng)新互聯(lián)成都企業(yè)網(wǎng)站建設(shè)服務(wù),提供網(wǎng)站設(shè)計、成都網(wǎng)站制作網(wǎng)站開發(fā),網(wǎng)站定制,建網(wǎng)站,網(wǎng)站搭建,網(wǎng)站設(shè)計,響應(yīng)式網(wǎng)站,網(wǎng)頁設(shè)計師打造企業(yè)風(fēng)格網(wǎng)站,提供周到的售前咨詢和貼心的售后服務(wù)。歡迎咨詢做網(wǎng)站需要多少錢:18982081108
frozenset() 是一種不變的集合,或者說該集合類型是hashable。
說明??: frozen 凍結(jié)的
>>> frozen_set
frozenset(['h', 'o', 'n', 'p', 't', 'y'])
>>> frozen_set.add("learn")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'frozenset' object has no attribute 'add'
說明??: 根據(jù)報錯信息來看,frozenset 集合不支持修改!
只有一種關(guān)系,元素要么屬于集合,要么不屬于。
>>> set1
set(['h', 'o', 'n', 'p', 't', 'y'])
>>> set2
set(['i', 'h', 'n', 'p', 't', 'y'])
>>> set1 == set2
False # set1,set2并不相等
如判斷集合A是否是集合B的子集,可以使用A<B,返回true則是子集,否則不是。也可以使用函數(shù)A.issubset(B)判斷。
>>> set1
set(['h', 'o', 'n', 'p', 't', 'y'])
>>> set3
set(['e', 'd', 'h', 'o', 'n', 'p', 't', 'y'])
>>> set1 < set3
True #set1 是 set3 的子集
或者使用issubset()函數(shù)進(jìn)行判斷:
>>> set1.issubset(set3)
True
>>> set3.issubset(set1)
False
>>> set2
set(['t', 'w', 'f'])
>>> set4
set(['a', 'h', 'z', 'o'])
>>> set2|set4 # 使用 “|” 得到就是兩個集合并集
set(['a', 't', 'w', 'f', 'h', 'z', 'o'])
或者使用union()函數(shù)進(jìn)行判斷:
>>> set2.union(set4)
set(['a', 't', 'w', 'f', 'h', 'z', 'o'])
>>> set5
set(['a', 'd', 't'])
>>> set6
set(['a', 'e', 'd', 't'])
>>> set5 & set6 # 使用“&” 得到兩個集合的交集
set(['a', 'd', 't'])
或者使用intersection()函數(shù)進(jìn)行判斷:
>>> set5.intersection(set6)
set(['a', 'd', 't'])
>>> set4
set(['a', 'h', 'z', 'o'])
>>> set6
set(['a', 'e', 'd', 't'])
>>> set4 - set6
set(['h', 'z', 'o'])
>>> set6 - set4
set(['e', 'd', 't'])
或者使用difference()函數(shù),如下:
>>> set4.difference(set6)
set(['h', 'z', 'o'])
>>> set6.difference(set4)
set(['e', 'd', 't'])
文章題目:python--集合概念和實戰(zhàn)(二)
網(wǎng)頁URL:http://m.kartarina.com/article27/geshcj.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、域名注冊、網(wǎng)站維護(hù)、用戶體驗、商城網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)