1.定義:無序不重復(fù)元素集, 基本功能包括關(guān)系測試和消除重復(fù)元素.
2.關(guān)鍵詞:類似dict、只有key無value、常用于集合類數(shù)學(xué)運(yùn)算、
3.創(chuàng)建
s=set() #入?yún)⒖梢允莑ist、tuple、str、甚至dict
示例:
>>> s=set([1,2,3])
>>> s
{1, 2, 3}
>>> s=set((1,2,3))
>>> s
{1, 2, 3}
>>> s=set('123')
>>> s
{'3', '2', '1'}
>>> s=set({'a':1,'b':2,'c':3})
>>> s
{'a', 'b', 'c'}
PS.
>>> s={} #此方法py默認(rèn)建立dict,而不是set
>>> type(s)
<class 'dict'>
4.集合添加、刪除
添加=兩種常用方法,分別是add和update。
add
>>> s=set('abc')
>>> s
{'a', 'b', 'c'}
>>> s.add('python') #add方法:是把要傳入的元素做為一個(gè)整個(gè)添加到集合中,入?yún)⑾薅看我粋€(gè)
>>> s
{'python', 'a', 'b', 'c'}
update(1):拆分添加
>>> s=set('abc')
>>> s.update('python') #update方法:是把要傳入的元素拆分,做為個(gè)體傳入到集合中
>>> s
{'h', 'n', 'y', 't', 'b', 'o', 'p', 'a', 'c'}
update(2):合并set
>>> set_1={1,2}
>>> set_2={'a','b'}
>>> set_3={'python','c#'}
>>> set_1.update(set_2,set_3)
>>> set_1
{1, 2, 'b', 'python', 'c#', 'a'}
常見錯(cuò)誤
>>> s=set([1,2,3])
>>> s
{1, 2, 3}
>>> s.add([4,5])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> #報(bào)錯(cuò)原因分析:在創(chuàng)建set時(shí),入?yún)⒖梢允莍terable,但創(chuàng)建完畢后,set內(nèi)部元素的type是key,key不能是可變類型,如list,謹(jǐn)記!
>>> s=set('abc')
>>> s
{'a', 'b', 'c'}
>>> s.update('de')
>>> s
{'d', 'a', 'b', 'e', 'c'}
>>> s.update(('f','g'))
>>> s
{'a', 'g', 'e', 'd', 'b', 'f', 'c'}
>>> s.update([1,2,3])
>>> s #update入?yún)⒁?guī)則同add,因?yàn)樗鼤?huì)打散入?yún)⒏袷?,所以只需審查入?yún)⒆钚挝?非可變 即可.
{'a', 'g', 1, 2, 3, 'e', 'd', 'b', 'f', 'c'}
>>> s.update([1,2,[4,5]]) #此處入?yún)⒆钚挝怀齣nt類1,2外還有一個(gè)[4,5]是list類型,所以報(bào)錯(cuò).
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
#set、dict類內(nèi)部結(jié)構(gòu)使用hash算法,遇到不可用hash算法類型入?yún)⒓磮?bào)錯(cuò)(unhashable)如list.
list/tuple/dict類均試驗(yàn)通過,唯獨(dú)str解釋不了,待查
>>> s=set([1,2])
>>> s
{1, 2}
>>> s.update('3,a,[4,5]')
>>> s
{1, 2, 'a', '4', '5', '[', '3', ']', ','}
#貌似update對入?yún)ype是str時(shí),將其內(nèi)部一切視為元素并打散,把[、]、逗號(hào)都認(rèn)成元素.而當(dāng)入?yún)ype是list、tuple卻不會(huì)。
刪除=s.discard(),s.pop(),s.remove() #del不支持set
>>> s={'Python','Java','C','C++','JavaScript','C#','Ruby','PHP','Objective-C'}
>>> s.remove('Java')
>>> s.remove('hello') # remove() 入?yún)⒉淮嬖趧t報(bào)錯(cuò)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
>>> s.discard('hello') #discard()入?yún)⒉淮嬖谝膊粓?bào)錯(cuò)
>>>
>>> s.pop() #pop()禁入?yún)?,隨機(jī)刪除并返回元素
'Ruby'
>>> s
{'C#', 'C++', 'Objective-C', 'PHP', 'C', 'Python', 'JavaScript'}
s.clear()清空set
>>> s
{'C#', 'C++', 'Objective-C', 'PHP', 'C', 'Python', 'JavaScript'}
>>> s.clear()
>>> s
set()
copy、issubset等等set函數(shù)用help(set.xx)查詢語法規(guī)則,用dir(set)查詢函數(shù)
5.set作為數(shù)學(xué)應(yīng)用的函數(shù)&操作符方法
1.測試單個(gè)元素在集合內(nèi)是否存在: inornot in
>>> s={1,2,3,4,5,'a','b','m','n'}
>>> 1 in s
True
>>> 7 in s
False
>>> 'c' not in s
True
2.測試兩個(gè)集合是否包含,子集操作:``issubset/issuperset
issubset=測試目標(biāo)set是否包含于入?yún)?>>> s1={2,3}
>>> s2={1,2,3,4,5}
>>> s1.issubset(s2)
True
>>> s2.issubset(s1)
False
issuperset=測試目標(biāo)set是否包含入?yún)?>>> s1={1,2,3,4,5}
>>> s2={2,3}
>>> s1.issuperset(s2)
True
3.合并兩個(gè)set并返回其并集set: set.union()
>>> s1={1,2,3,4,5}
>>> s2={2,3,7}
>>> s1.union(s2)
{1, 2, 3, 4, 5, 7} #set為無序無重復(fù)集合,自動(dòng)摒除重復(fù)元素
>>> s3=s1.union(s2)
>>> s3
{1, 2, 3, 4, 5, 7}
________________________________
操作符求并集:s1|s2
>>> s=s1|s2
>>> s
{1, 3, 4, 5, 6, 7}
4.交集:intersection()
>>> s1={1,3,4,5,7}
>>> s2={3,4,6}
>>> s1.intersection(s2)
{3, 4}
______________________________
操作符求交集:s1&s2
>>> s1&s2
{3, 4}
5.其他:difference() 相當(dāng)于'差集'運(yùn)算符
>>> s1={1,2,3,4,5}
>>> s2={2,4,6,8}
>>> s1.difference(s2) #s1中異于s2的元素集合
{1, 3, 5}
______________________________
#difference()=s1-s2 (差集操作符):相當(dāng)于s1中減去s1&s2
>>> s1-s2
{1, 5, 7}
symmetric_difference():對稱差集
>>> s1={1,2,3,4,5}
>>> s2={2,4,6,8}
>>> s1.symmetric_difference(s2)
{1, 3, 5, 6, 8}
_____________________________
操作符:s1^s2 #對稱差集,相當(dāng)于s1|s2-s1&s2
>>> s1^s2
{1, 3, 5, 6, 8}
6.set遍歷
#基本遍歷法同list
s={1, 2, 'd', 'a', '4', 'c', '5'}
for i in s:
print(i)
______________________________
#idx表示集合中元素i的索引
s={1, 2, 'd', 'a', '4', 'c', '5'}
for idx, i in enumerate(s):
print(idx,i)