程式扎記: [Python Std Library] Built-in Types : Set Types

標籤

2012年3月12日 星期一

[Python Std Library] Built-in Types : Set Types

翻譯自 這裡 
Set Types — setfrozenset 
A set object is an unordered collection of distinct hashable objects. Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference. (For other containers see the built in dictlist, and tuple classes, and thecollections module.

Like other collections, sets support x in setlen(set), and for x in set. Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior. 

There are currently two built-in set types, set and frozenset. The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set. The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set. 

As of Python 2.7, non-empty sets (not frozensets) can be created by placing a comma-separated list of elements within braces, for example: {'jack', 'sjoerd'}, in addition to the set constructor. 

Methods/Operations in Set : 
看完上面 Set 類型物件的說明, 知道 set 與 fronzenset 內的物件必須是 hashable; 而傳入它們建構子的物件必須是 iterable. 如果你想用讓 set 中包含 set 類型物件, 則必須使用 frozenset 物件而非 set 物件, 原因是 frozenset 是 hashable 而 set 不是. 接著我們來看看這兩個物件上面有提供什麼方法與操作 : 
len(s) 
返回 s 中有幾個物件或元素, 不包含元素中的子元素!
>>> s = set([1, 2, '3', frozenset([4, 5])]) # set 中的 set 必須使用 frozenset.
>>> s
{1, 2, '3', frozenset({4, 5})}
>>> len(s)
4

x in sx not in s 
檢視 x (不)是 s 的一個 member.
>>> s = set([1, 2, '3', frozenset([4, 5])])
>>> 1 in s
True
>>> 3 in s
False
>>> 4 in s
False
>>> fz = frozenset([4, 5])
>>> fz in s
True

s.isdisjoint(other) (New in version 2.6.) 
檢視 other 中的 members 是否與 s 為 disjoint ; 即 other 中的 members 都沒有出現在 s.
>>> s1 = set([1, 2, 3])
>>> s2 = set([4, 5, 6])
>>> s3 = set([2, 7, 8])
>>> s1.isdisjoint(s2)
True
>>> s1.isdisjoint(s3)
False

s.issubset(other) | set <= other 
決定 s 是否為 other 的一個子集合. 即 s ∈ other 則 True, 否則 False.
>>> s = set([1, 3, 5])
>>> other1 = set([1, 3, 5, 7])
>>> other2 = set([1, 3, 11])
>>> s.issubset(other1)
True
>>> s.issubset(other2)
False

s.issuperset(other) | set >= other 
判斷 other ∈ s.
>>> s = set([1, 3, 5, 7])
>>> other1 = set([1, 3])
>>> other2 = set([1, 3, 5, 7, 9])
>>> s.issuperset(other1)
True
>>> s.issuperset(other2)
False

s.union(other, ...) | set | other | ... 

對 other 與 s 進行 union (n個集合的聯集).
>>>
 s = set([1, 3, 5])
>>> s2 = set([1, 5, 7])
>>> s3 = set([1, 7, 11])
>>> s.union(s2, s3)
{1, 3, 5, 7, 11}

s.intersection(other, ...) | set & other & ... 
對 n 個集合進行交集 (sother, ...) :
>>> s1 = set([1, 2, 3, 4])
>>> s2 = set([3, 4, 5, 6])
>>> s1.intersection(s2)
{3, 4}

s.difference(other, ...) | set - other - ... 
取出 s 與 other 等其他集合的差集. (即 s - other - ...)
>>> s1 = set([1, 2, 3, 4, 5, 6])
>>> s2 = set([2, 4])
>>> s3 = set([3, 4, 5])
>>> s1.difference(s2, s3)
{1, 6}

s.symmetric_difference(other) | set ^ other 
取 s 與 other 的 XOR. (s ^ other)
>>> s1 = set([1, 2, 3, 4, 5])
>>> s2 = set([3, 4, 5, 6])
>>> s1.symmetric_difference(s2)
{1, 2, 6}

s.copy() 
返回 s 集合的 shallow copy.

在進行產生新的集合的操作時, 如果運算元是由 set 與 frozenset 組成, 則返回的類型由第一個運算元決定. 例如 frozenset('ab') | set('bc') 會返回 frozenset 的物件. 底下的方法操作會變動集合中的元素, 因此只有 mutable 的 set 類型物件支援 : 
s.update(other, ...) | set |= other | ... 
對 other, ... 集合中, s 集合沒有的元素進行添加 :
>>> s1 = set([1, 2, 3])
>>> s1.update([2, 3, 4])
>>> s1
{1, 2, 3, 4}

s.intersection_update(other, ...) | set &= other & ... 
將 與 other, ... 進行交集後更新到集合 s.
>>> s1 = set([1, 2, 3])
>>> s2 = set([2, 3, 4])
>>> s1.intersection_update(s2) # s1 與 s2 共有的 members 有 [2, 3]
>>> s1
{2, 3}

s.difference_update(other, ...) | s -= other | ... 
將 other 與 ... 中有的元素從 s 移除.
>>> s1 = set([1, 2, 3, 4, 5])
>>> other = set([1, 3, 5])
>>> s1.difference_update(other)
>>> s1
{2, 4}

s.symmetric_difference_update(other) | s ^= other 
將 s 有, other 沒有的元素與 other 有, s 沒有的元素保留在 s 中.
>>> s1 = set([1, 2, 3])
>>> other = set([2, 3, 4]) other 有 s 沒有為 [4]; s 有 other 沒有為 [1].
>>> s1.symmetric_difference_update(other)
>>> s1
{1, 4}

s.add(elem) 
添加 elem 到 集合 s. (如果s 集合已經有 elem, 則不會添加.)
>>> s1 = set([1, 2])
>>> s1.add(4)
>>> s1
{1, 2, 4}
>>> s1.add(1)
>>> s1
{1, 2, 4}

s.remove(elem) 
移除集合 s 中的元素 elem, 如果該 member 不存在, 則丟出 KeyError.
>>> s1 = set([1, 2])
>>> s1.remove(1)
>>> s1
{2}
>>> s1.remove(3)
Traceback (most recent call last):
File "", line 1, in
KeyError: 3

s.discard(elem) 
與 s.remove() 相同, 只是這個版本不會丟出 Exception 即使 elem 不存在於集合中.

s.pop() 
隨意從集合 s 移出一個 member; 如果集合為空, 則丟出 KeyError 例外.
>>> s1 = set([1, 2])
>>> s1.pop()
1
>>> s1.pop()
2
>>> s1.pop()
Traceback (most recent call last):
File "", line 1, in
KeyError: 'pop from an empty set'

s.clear() 
清空集合 s 中的所有 member.
>>> s1 = set([1, 2])
>>> s1.clear()
>>> s1
set()

至於集合間的比對可以參考 Comparison to the built-in set types.

沒有留言:

張貼留言

網誌存檔

關於我自己

我的相片
Where there is a will, there is a way!