## 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 :

len(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

>>> 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.)

>>> 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 = 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

>>> 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 | ...

>>>
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 & ...

>>> s1 = set([1, 2, 3, 4])
>>> s2 = set([3, 4, 5, 6])
>>> s1.intersection(s2)
{3, 4}

s.difference(other, ...) | set - 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

>>> s1 = set([1, 2, 3, 4, 5])
>>> s2 = set([3, 4, 5, 6])
>>> s1.symmetric_difference(s2)
{1, 2, 6}

s.copy()

s.update(other, ...) | set |= other | ...

>>> s1 = set([1, 2, 3])
>>> s1.update([2, 3, 4])
>>> s1
{1, 2, 3, 4}

s.intersection_update(other, ...) | set &= other & ...

>>> 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 | ...

>>> 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

>>> s1 = set([1, 2, 3])
>>> other = set([2, 3, 4]) other 有 s 沒有為 [4]; s 有 other 沒有為 [1].
>>> s1.symmetric_difference_update(other)
>>> s1
{1, 4}

>>> s1 = set([1, 2])
>>> s1
{1, 2, 4}
>>> s1
{1, 2, 4}

s.remove(elem)

>>> s1 = set([1, 2])
>>> s1.remove(1)
>>> s1
{2}
>>> s1.remove(3)
Traceback (most recent call last):
File "", line 1, in
KeyError: 3

s.pop()

>>> 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()

>>> s1 = set([1, 2])
>>> s1.clear()
>>> s1
set()

## 關於我自己

Where there is a will, there is a way!