Python集合set和frozenset的內(nèi)建方法詳解
前言
集合是一種組合型的數(shù)據(jù)類型,分為可變的set和不可變的frozenset。
軟件環(huán)境
- 系統(tǒng)
- UbuntuKylin 14.04
- 軟件
- Python 2.7.3
- IPython 4.0.0
可變集合Set
集合set是一種無序的、唯一的的元素集,與數(shù)學中集合的概念類似,可對其進行交、并、差、補等邏輯運算。不支持索引、切片等序列操作,但仍支持成員關(guān)系運算符in-not in、推導式等操作。在特定的場合中可以體現(xiàn)出非常優(yōu)秀的執(zhí)行效率。
set()函數(shù)創(chuàng)建集合
- set(iterable) -> new set object
其中iterable可以是List、Tuple、Dictionary。但是為dict時,只會獲取提Key作為set的元素。
- n [12]: s = set([1,2,3])
- In [13]: s
- Out[13]: {1, 2, 3}
- In [14]: type(s)
- Out[14]: set
- In [141]: s2 = set('jmilk')
- In [142]: s2
- Out[142]: {'i', 'j', 'k', 'l', 'm'}
注意:set()函數(shù)只能接受迭代器(String、Tuple、List、Dict、set)作為參數(shù)。
- In [180]: s1 = set(1)
- ---------------------------------------------------------------------------
- TypeError Traceback (most recent call last)
- <ipython-input-180-8804a520da97> in <module>()
- ----> 1 s1 = set(1)
- TypeError: 'int' object is not iterable
傳遞一個非迭代器參數(shù)時會報錯。
創(chuàng)建空集合
- set() -> new empty set object
通過上面的例子,可見set類型數(shù)據(jù)和dict類型一樣也是使用{}來標識,但是需要注意的是:dict類型可以使用dic = {}來創(chuàng)建一個空字典,set類型卻不能,只能通過s = set()來創(chuàng)建。
- In [15]: s = set()
- In [16]: s
- Out[16]: set()
- In [17]: d = {}
- In [18]: d
- Out[18]: {}
- In [20]: type(s),type(d)
- Out[20]: (set, dict)
注意:因為set()是一個可變的集合,其元素的數(shù)量是不固定的,所以有add()、remove()方法。但也因為其可變性所以set()類型的對象沒有散列值(哈希值),同時也不能作為dict對象的key和其他set對象的元素。
哈希值:將長度不一的輸入數(shù)據(jù)源,通過算法轉(zhuǎn)換為長度一致的數(shù)據(jù)輸出,以此來提高查找速度。
MD5:對文件或者數(shù)據(jù)源(字符串、數(shù)值)進行計算后得到一個固定的值,用來驗證文件或數(shù)據(jù)源是否被篡改。一般用于文件的數(shù)字簽名。
集合元素的唯一性
無論是set還是frozenset中的元素都是唯一的,會自動合并重疊元素。
- In [17]: li = [1,2,3,1,1,2,'a','b','a']
- In [18]: s1 = set(li)
- In [19]: s2 = frozenset(li)
- In [20]: s1,s2
- Out[20]: ({1, 2, 3, 'a', 'b'}, frozenset({1, 2, 3, 'a', 'b'}))
集合推導式
由一個迭代器推倒出一個新的集合。
- In [175]: set(x**2 for x in range(1,6) if x < 4)
- Out[175]: {1, 4, 9}
set類型對象的內(nèi)置方法
add()增加一個元素
add(…)
Add an element to a set.
This has no effect if the element is already present.
增加一個元素到set對象中,如果這個元素已經(jīng)存在,則沒有效果。
- In [41]: s1 = set(range(10))
- In [42]: s1
- Out[42]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
- In [43]: s1.add(10)
- In [44]: s1
- Out[44]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
remove()刪除一個元素
remove(…)
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
指定刪除set對象中的一個元素,如果集合中沒有這個元素,則返回一個錯誤。
- In [47]: s1
- Out[47]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
- In [48]: s1.remove(0)
- In [49]: s1
- Out[49]: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
一次只能刪除i個元素。
pop()隨機刪除并返回一個元素
pop(…)
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
隨機刪除并返回一個集合中的元素,若集合為空,則返回一個錯誤。
- In [65]: s2 = set(['j','m','i','l','k'])
- In [66]: s2.pop()
- Out[66]: 'i'
- In [67]: s2.pop()
- Out[67]: 'k'
discard()刪除一個元素
discard(…)
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
指定刪除集合中的一個元素,若沒有這個元素,則do nothing。
- In [90]: s1
- Out[90]: {1, 2, 3, 4, 5, 6, 7, 8, 9}
- In [91]: s1.discard(1)
- In [92]: s1
- Out[92]: {2, 3, 4, 5, 6, 7, 8, 9}
- In [93]: s1.discard('abc')
- In [94]: s1
- Out[94]: {2, 3, 4, 5, 6, 7, 8, 9}
clear()
clear(…)
Remove all elements from this set.
清空一個集合中的所有元素
- In [94]: s1
- Out[94]: {2, 3, 4, 5, 6, 7, 8, 9}
- In [95]: s1.clear()
- In [96]: s1
- Out[96]: set()
注意:上面列出的函數(shù)都是可變類型set對象獨有的函數(shù),除此之外還有一些set和frozenset共有的內(nèi)置函數(shù),我們后面再介紹。
不可變集合Frozenset
frozenset凍結(jié)集合,即不可變集合。frozenset的元素是固定的,一旦創(chuàng)建后就無法增加、刪除和修改。其***的優(yōu)點是使用hash算法實現(xiàn),所以執(zhí)行速度快,而且frozenset可以作為dict字典的Key,也可以成為其他集合的元素。
frozenset()創(chuàng)建一個frozenset集合
- frozenset(object)
- frozenset() -> empty frozenset object
- frozenset(iterable) -> frozenset object
- Build an immutable unordered collection of unique elements.
創(chuàng)建的固定的無序集合
- In [108]: f1 = frozenset() #空的frozenset集合
- In [109]: f2 = frozenset([1,2,3,'JMilk'])
- In [110]: f1,f2
- Out[110]: (frozenset(), frozenset({1, 2, 3, 'JMilk'}))
set能夠與frozenset作比較
- In [4]: s1 = set([1,2,3])
- In [5]: s2 = frozenset([1,2,3])
- In [6]: s1 == s2
- Out[6]: True
set和frozenset的混合運算
兩種類型集合之間的混合運算會返回***個操作元素的類型。
- In [12]: s1 = set([1,2,3])
- In [13]: s2 = frozenset([2,3,4])
- In [14]: s3 = s1 | s2
- In [15]: s3,type(s3)
- Out[15]: ({1, 2, 3, 4}, set)
frozenset集合作為dic的key
- In [138]: f = frozenset(['name'])
- In [139]: dic = {f:'Jmilk'}
- In [140]: dic
- Out[140]: {frozenset({'name'}): 'Jmilk'}
set集合不可以作為Dictionary的Key:
- In [144]: s1 = set(['JMilk'])
- In [145]: dic = {s1:'name'}
- ---------------------------------------------------------------------------
- TypeError Traceback (most recent call last)
- <ipython-input-145-a2aec7bb3b32> in <module>()
- ----> 1 dic = {s1:'name'}
- TypeError: unhashable type: 'set' #不具有hash
set、frozenset共有的內(nèi)建函數(shù)
set與frozenset類型的集合都支持集合之間的比較、交、并、差操作,類似數(shù)據(jù)的集合關(guān)系比較。但是需要注意的是:因為frozenset是不可變集合,所以下列函數(shù)中帶有’_update’關(guān)鍵字的函數(shù),frozenset都不可以調(diào)用。
NOTE: 帶有 _update 的函數(shù),使用原位操作的方法實現(xiàn),擁有更低的資源消耗。但是這樣的話,函數(shù)是沒有返回值的,即不能將結(jié)果賦值給一個新的變量。
intersection()、intersection_update()求交集
intersection(…)
Return the intersection of two or more sets as a new set.
返回一個由若干個集合經(jīng)過交集運算后得到的新交集,可以傳入多個迭代器類型的參數(shù)。即可以傳遞Tuple、List、、String、Dictionary、Set等類型參數(shù)。
集合之間求交集
- In [59]: s1
- Out[59]: {1, 2, 3, 'a', 'b'}
- In [60]: s2
- Out[60]: {3, 4, 5, 'b', 'c', 'd'}
- In [61]: s3 = set([1,3,8,9,10,'d''e','f'])
- In [62]: s4 = s1.intersection(s2,s3)
- In [63]: s4
- Out[63]: {3}
注意:也可以使用邏輯與運算符 ‘&’
- In [28]: s3 = s1 & s2
- In [29]: s3
- Out[29]: {3, 'b'}
集合和列表求交集
- In [64]: li
- Out[64]: [1, 2, 3, 1, 1, 2, 'a', 'b', 'a']
- In [65]: s4 = s1.intersection(li)
- In [66]: s4
- Out[66]: {1, 2, 3, 'a', 'b'}
集合和元組求交集
- In [67]: tup = (2,'a','c')
- In [68]: s4 = s1.intersection(tup)
- In [69]: s4
- Out[69]: {2, 'a'}
集合和字符串求交集
注意:只能以String中的字符進行相交運算,不能與String的數(shù)字做運算。
- In [70]: str = '123abc'
- In [71]: s4 = s1.intersection(str)
- In [72]: s4
- Out[72]: {'a', 'b'}
- In [79]: s1
- Out[79]: {1, 2, 3, 'a', 'b'}
集合和字典求交集
注意:只能與字典中的Key進行相交運算。
- In [79]: s1
- Out[79]: {1, 2, 3, 'a', 'b'}
- In [80]: dic = {1:'name',2:'age'}
- In [81]: s4 = s1.intersection(dic)
- In [82]: s4
- Out[82]: {1, 2}
intersection()與intersection_update()的區(qū)別
intersection_update(…)
Update a set with the intersection of itself and another.
更新一個經(jīng)過相交后的集合給自己。
注意:當我們希望將兩個對象相交后的結(jié)果更新給其中一個操作對象時,建議使用intersection_update()函數(shù),這個函數(shù)使用原位操作的方法實現(xiàn),擁有更低的資源消耗。但是intersection_update()函數(shù)是沒有返回值的,即不能將結(jié)果賦值給一個新的變量。
- In [94]: s1
- Out[94]: {1, 2, 3, 'a', 'b'}
- In [95]: s2
- Out[95]: {3, 4, 5, 'b', 'c', 'd'}
- In [96]: s1.intersection_update(s2)
- In [97]: s1
- Out[97]: {3, 'b'}
union()、update()求并集
與intersection()一樣,可以傳遞不同的迭代器類型參數(shù)。
union() 返回并集
union(…)
Return the union of sets as a new set.
- In [108]: s4
- Out[108]: {1, 2, 3, 4, 5, 8, 9, 10, 'a', 'b', 'c', 'd', 'de', 'f'}
注意:可以使用邏輯或運算符 ‘|’
- In [109]: s4 = s1 | s2 | s3
- In [110]: s4
- Out[110]: {1, 2, 3, 4, 5, 8, 9, 10, 'a', 'b', 'c', 'd', 'de', 'f'}
update()更新并集
update(…)
Update a set with the union of itself and others.
update()方法沒有返回值。
- In [111]: s1.update(s2,s3)
- In [112]: s1
- Out[112]: {1, 2, 3, 4, 5, 8, 9, 10, 'a', 'b', 'c', 'd', 'de', 'f'}
difference()、difference_update()求差
difference()
difference(…)
Return the difference of two or more sets as a new set.
返回由一個集合中不存在于其他若干個集合的元素組成的新集合。
- In [122]: s1
- Out[122]: {1, 2, 3, 'a', 'b'}
- In [123]: s2
- Out[123]: {3, 4, 5, 'b', 'c', 'd'}
- In [124]: s3 = s1.difference(s2)
- In [125]: s3
- Out[125]: {1, 2, 'a'}
注意:可以使用算術(shù)運算符減 ‘-‘
- In [126]: s3 = s1 - s2
- In [127]: s3
- Out[127]: {1, 2, 'a'}
difference_update()
difference_update(…)
Remove all elements of another set from this set.
更新原來集合。
- In [130]: s1.difference_update(s2)
- In [131]: s1
- Out[131]: {1, 2, 'a'}
symmetric_difference()、symmetric_difference_update()求集合彼此之差的并集
symmetric_difference()
symmetric_difference(…)
Return the symmetric difference of two sets as a new set.
即返回(set1 – set2)|(set2 – set1)的結(jié)果
- In [138]: s1
- Out[138]: {1, 2, 3, 'a', 'b'}
- In [139]: s2
- Out[139]: {3, 4, 5, 'b', 'c', 'd'}
- In [140]: s3 = s1.symmetric_difference(s2)
- In [141]: s3
- Out[141]: {1, 2, 4, 5, 'a', 'c', 'd'}
等效于:
- In [147]: s1 - s2
- Out[147]: {1, 2, 'a'}
- In [148]: s2 - s1
- Out[148]: {4, 5, 'c', 'd'}
- In [144]: s3 = (s1 - s2)|(s2 - s1)
- In [145]: s3
- Out[145]: {1, 2, 4, 5, 'a', 'c', 'd'}
注意:可以使用^來代替
- In [142]: s3 = s1 ^ s2
- In [143]: s3
- Out[143]: {1, 2, 4, 5, 'a', 'c', 'd'}
symmetric_difference_update()
symmetric_difference_update(…)
Update a set with the symmetric difference of itself and another.
- In [150]: s1.symmetric_difference_update(s2)
- In [151]: s1
- Out[151]: {1, 2, 4, 5, 'a', 'c', 'd'}
集合間的關(guān)系
相等:只有每一個一個set都互相是另一個set的子集時,這兩個set才相等。
小于(set1包含于set2):只有當***個set1是另一個set2的子集,別且兩個set不相等時,***個set1小于第二個set2。
大于(set1包含set2):只有***個set1是第二個set2的超集、并且兩者不相等時,***個set2大于第二個set2。
isdisjoint()兩個集合不相交
isdisjoint(…)
Return True if two sets have a null intersection.
即set1 & set2 == set() 時,為True
- In [155]: s1
- Out[155]: {1, 2, 4, 5, 'a', 'c', 'd'}
- In [156]: s2
- Out[156]: {3, 4, 5, 'b', 'c', 'd'}
- In [158]: (s1 - s2) & s2 == set()
- Out[158]: True
- In [159]: s2.isdisjoint(s1-s2)
- Out[159]: True
issuperset()一個集合包含另一個集合
issuperset(…)
Report whether this set contains another set.
- In [169]: s1
- Out[169]: {0, 1, 2}
- In [170]: s2
- Out[170]: {0, 1, 2, 3, 4}
- In [172]: s2.issuperset(s1)
- Out[172]: True
s2大于s1
issubset()一個集合包含于另一個集合
issubset(…)
Report whether another set contains this set.
- In [169]: s1
- Out[169]: {0, 1, 2}
- In [170]: s2
- Out[170]: {0, 1, 2, 3, 4}
- In [171]: s1.issubset(s2)
- Out[171]: True
s1被s2包含
集合的數(shù)據(jù)類型轉(zhuǎn)換
主要轉(zhuǎn)換為序列類型。
- In [1]: set1 = set(range(5))
- In [2]: li = list(set1)
- In [3]: tup = tuple(set1)
- In [4]: string = str(set1)
- In [5]: li,tup,string
- Out[5]: ([0, 1, 2, 3, 4], (0, 1, 2, 3, 4), 'set([0, 1, 2, 3, 4])')
***
集合是一個非常有意思的數(shù)據(jù)結(jié)構(gòu),他或許不被經(jīng)常使用,但是在比較嚴格的執(zhí)行環(huán)境下,集合是一個非常好的選擇。