python3:set 和 frozenset的應用場景及區別
set 是集合,frozenset 是凍結的集合,顧名思義是不可變集合。
set 最大的特性是不重合,在去重的時候用的最多。
1.接受一個可迭代的型別
先簡單的看下class 的說明如下:
class set(object):
"""
set() -> new empty set object
set(iterable) -> new set object
Build an unordered collection of unique elements.
set(iterable)它接受一個可迭代的型別,所以我們就大膽的用吧
可以傳入字串,list ,dict ,tuple
a=set("abcde")
print(a)
b=set([1,2,3])
print(b)
c=set({"a":1,"b":2})
print(c)
d=set((1,2))
print(d)
列印結果:
{'b', 'd', 'a', 'e', 'c'}
{1, 2, 3}
{'b', 'a'}
{1, 2}
2.無序性和dict區別
從上邊的列印結果 {‘b’, ‘d’, ‘a’, ‘e’, ‘c’}可以證明是無序性。
大家注意下,他和dict 極為相似,但是卻屬於set型別,自己分辨下。大家都知道
dict語法是{key:value,key:value} 是這個格式,下邊用type 打印出看看是什麼型別?
a={"a","b","c"}
print(type(a))
列印結果:
<class 'set'>
3.set和frozenset 的區別
剛開始也講了frozenset是不可變的,如果修改是報錯的,那到底有什麼用處呢
應用場景:一般dict 的key 是不變的,我們就可以用
那我們用程式碼證明下,set不會報錯,frozenset 會報錯.
先改變set型別
a={"a","b","c"}
a.add("d")
print((a))
列印結果:
{'c', 'a', 'd', 'b'}
修改frozenset會報錯 如下:
a=frozenset("abcd") print(a) a.add("d") print((a))
列印結果:
frozenset({'b', 'c', 'a', 'd'})
AttributeError: 'frozenset' object has no attribute 'add'
4.set方法介紹
set 的方法非常多, 我擷取原始碼一些方法,然後挑幾個講解下
def clear(self, *args, **kwargs): # real signature unknown
def copy(self, *args, **kwargs): # real signature unknown
def difference(self, *args, **kwargs): # real signature unknown
"""
Return the difference of two or more sets as a new set.
def difference_update(self, *args, **kwargs): # real signature unknown
""" Remove all elements of another set from this set. """
pass
def discard(self, *args, **kwargs): # real signature unknown
"""
Remove an element from a set if it is a member.
If the element is not a member, do nothing.
"""
pass
def intersection(self, *args, **kwargs): # real signature unknown
"""
Return the intersection of two sets as a new set.
(i.e. all elements that are in both sets.)
"""
pass
def intersection_update(self, *args, **kwargs): # real signature unknown
""" Update a set with the intersection of itself and another. """
pass
def pop(self, *args, **kwargs): # real signature unknown
def remove(self, *args, **kwargs): # real signature unknown
def union(self, *args, **kwargs): # real signature unknown
"""
Return the union of sets as a new set.
(i.e. all elements that are in either set.)
"""
pass
def update(self, *args, **kwargs): # real signature unknown
""" Update a set with the union of itself and others. """
pass
1. update 方法
原始碼裡的描述很清晰( “”" Update a set with the union of itself and others. “”")
意思就是聯合兩個set,取的是總集。
看例子:
a=set("abc")
a.update("bcd")
print(a)
列印結果:
{'b', 'a', 'c', 'd'}
把兩個set 集合在一起並去重。
2. difference 的用法
先看方法描述吧 ( Return the difference of two or more sets as a new set.)
用例子加深印象。
a=set("abc")
print(a.difference("bcd"))
print(a)
列印結果:
{'a'}
{'c', 'a', 'b'}
呼叫方法之後直接返回一個不同的元素。
看第二個輸出他不會對原set 進行改變。
其他的方法自己應用下,應該都不難。
5. set集合運算(|,&,-)
看例項吧
a=set("abc")
b=set("bcd")
print(a-b) #對a進行去重
print(a | b) #並集
print(a & b) #交集
列印結果:
{'a'}
{'d', 'c', 'b', 'a'}
{'c', 'b'}
之所以可以這麼操作,原理他是呼叫了相應的魔法函式。
例如 :
“-” 呼叫的是
def __sub__(self, *args, **kwargs): # real signature unknown
""" Return self-value. """
pass
“|” 呼叫的是
def __or__(self, *args, **kwargs): # real signature unknown
""" Return self|value. """
pass
“&”呼叫的是
def __and__(self, *args, **kwargs): # real signature unknown
""" Return self&value. """
pass
總結:
frozenset和set 區別是可變不可變,可接受可迭代的引數.
set的效能比較高 因為他用到了雜湊編碼. set 的集合運算也要看下,非常重要.