set v.s. frozenset
A set object is an unordered collection of distinct hashable objects.
- 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.
Class
class set([iterable])
class frozenset([iterable])
Return a new set or frozenset object whose elements are taken from iterable. The elements of a set must be hashable. To represent sets of sets, the inner sets must be frozenset objects. If iterable is not specified, a new empty set is returned.
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.
Note: to create an empty set you have to use set()
, not{}
; the latter creates an empty dictionary.
>>> a = {}
>>> type(a)
<class 'dict'>
>>>
>>> a = set()
>>> type(a)
<class 'set'>
set and frozenset 's Methods
Membership
-
In a mathematics
- If B is a set and x is one of the objects of B, this is denoted x ∈ B, and is read as "x belongs to B", or "x is an element of B".
- If y is not a member of B then this is written as y ∉ B, and is read as "y does not belong to B".
-
In Python
x in s
Test x for membership in s.x not in s
Test x for non-membership in s.
>>> a = {1, 2, 3}
>>> 1 in a
True
>>> 4 in a
False
>>> 4 not in a
True
Subsets
-
In mathematics
If every member of set A is also a member of set B, then A is said to be a subset of B, written A ⊆ B (also pronounced A is contained in B). Equivalently, we can write B ⊇ A, read as B is a superset of A, B includes A, or B contains A.
If A is a subset of, but not equal to, B, then A is called a proper subset of B, written A ⊊ B (A is a proper subset of B) or B ⊋ A (B is a proper superset of A).
-
In Python
-
issubset(other)
- set <= other
Test whether every element in the set is in other. - set < other
Test whether the set is a proper subset of other, that is, set <= other and set != other
.
- set <= other
-
issuperset(other)
set >= other
Test whether every element in other is in the set.set > other
Test whether the set is a proper superset of other, that is, set >= other and set != other
.
-
>>> a = {1, 2, 3}
>>> b = {1, 2}
>>> c = {3, 4}
>>> d = {1, 2, 3}
>>>
>>> b.issubset(a)
True
>>> c.issubset(a)
False
>>> a.issuperset(b)
True
>>> a.issubset(d)
True
>>> d.issubset(a)
True
Cardinality
-
In mathematics
- The cardinality | S | of a set S is "the number of members of S."
-
In Python
- len(s)
Return the number of elements in set s (cardinality of s).
- len(s)
>>> a = {1, 2, 3}
>>> len(a)
3
Union
-
In mathematics
- Two sets can be "added" together. The union of A and B, denoted by A ∪ B, is the set of all things that are members of either A or B.
-
In Python
- union(*others)
set | other | ...
Return a new set with elements from the set and all others.
- union(*others)
>>> a = {1, 2, 3}
>>> b = {3, 4}
>>>
>>> a.union(b)
{1, 2, 3, 4}
>>> a | b
{1, 2, 3, 4}
Intersections and Disjoint
-
In mathematics
- A new set can also be constructed by determining which members two sets have "in common". The intersection of A and B, denoted by A ∩ B, is the set of all things that are members of both A and B.
- If A ∩ B = ∅, then A and B are said to be disjoint.
-
In Python
- intersection(*others)
set & other & ...
Return a new set with elements common to the set and all others. - isdisjoint(other)
Return True if the set has no elements in common with other. Sets are disjoint if and only if their intersection is the empty set.
- intersection(*others)
>>> a = {1, 2, 3}
>>> b = {3, 4}
>>>
>>> a.intersection(b)
{3}
>>> a & b
{3}
>>> a.isdisjoint(b)
False
Difference
-
In mathematics
- If A and B are sets, then the relative complement of A in B, also termed the set-theoretic difference of B and A, is the set of elements in B but not in A.denoted by B \ A (or B − A). Note that it is valid to "subtract" members of a set that are not in the set.
-
In Python
- difference(*others)
set - other - ...
Return a new set with elements in the set that are not in the others.
- difference(*others)
>>> a = {1, 2, 3}
>>> b = {3, 4}
>>>
>>> a - b
{1, 2}
>>> a.difference(b)
{1, 2}
Symmetric difference
-
In mathematics
- The symmetric difference, also known as the disjunctive union, of two sets is the set of elements which are in either of the sets and not in their intersection.
-
In Python
- symmetric_difference(*other)
set ^ other
Return a new set with elements in either the set or other but not both.
- symmetric_difference(*other)
>>> a = {1, 2, 3}
>>> b = {3, 4}
>>>
>>> a.symmetric_difference(b)
{1, 2, 4}
>>> a ^ b
{1, 2, 4}
copy
Return a new set with a shallow copy of s.
>>> a
{1, 2, 3}
>>> b = a.copy
>>> id(a) == id(b)
False
some other methods(only for set)
The following table lists operations available for set that do not apply to immutable instances of frozenset:
- update(*others)
set |= other | ...
Update the set, adding elements from all others.
>>> a
{1, 2, 3}
>>> b
{3, 4}
>>> id_before = id(a)
>>> a |= b
>>> a
{1, 2, 3, 4}
>>> id_before == id(a)
True
- intersection_update(*others)
set &= other & ...
Update the set, keeping only elements found in it and all others.
>>> a
{1, 2, 3}
>>> b
{3, 4}
>>> a = {1, 2, 3}
>>> a &= b
>>> a
{3}
- difference_update(*others)
set -= other | ...
Update the set, removing elements found in others.
>>> a = {1, 2, 3}
>>> b
{3, 4}
>>> a -= b
>>> a
{1, 2}
- symmetric_difference_update(other)
set ^= other
Update the set, keeping only elements found in either set, but not in both.
>>> a = {1, 2, 3}
>>> b
{3, 4}
>>> a ^= b
>>> a
{1, 2, 4}
- add(elem)
Add element elem to the set.
>>> a = {1, 2, 3}
>>>
>>> a.add(4)
>>> a
{1, 2, 3, 4}
- remove(elem)
Remove element elem from the set. RaisesKeyError
if elem is not contained in the set.
>>> a
{1, 2, 3, 4}
>>> a.remove(4)
>>> a
{1, 2, 3}
>>> a.remove(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 4
- discard(elem)
Remove element elem from the set if it is present.
>>> a
{1, 2, 3}
>>> a.discard(4)
>>> a.discard(2)
>>> a
{1, 3}
- pop()
Remove and return an arbitrary element from the set. RaisesKeyError
if the set is empty.
>>> a = {1}
>>> a.pop()
1
>>> a.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'pop from an empty set'
- clear()
Remove all elements from the set.
>>> a = {1, 2}
>>>
>>> a.clear()
>>> a
set()
Note
- non-operator v.s. operator
the non-operator versions of union(), intersection(), difference(), and symmetric_difference(), issubset(), and issuperset() methods will accept any iterable as an argument. In contrast, their operator based counterparts require their arguments to be sets.
>>> a = {1, 2, 3}
>>> c = [2, 4]
>>>
>>> a.union(c)
{1, 2, 3, 4}
>>> a | c
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'set' and 'list'
- comparions
Both set and frozenset support set to set comparisons.- Two sets are equal if and only if every element of each set is contained in the other (each is a subset of the other).
- A set is less than another set if and only if the first set is a proper subset of the second set (is a subset, but is not equal).
- A set is greater than another set if and only if the first set is a proper superset of the second set (is a superset, but is not equal).
>>> a = {1, 2, 3}
>>> b = {1, 2}
>>> c = {3, 4}
>>> a > b
True
>>> a > c
False
>>> d = {1, 2, 3}
>>> a == d
True
- {} v.s. set()
Why is that? Haaaa...O!
>>> a = {'abc'}
>>> a
{'abc'}
>>> b = set('abc')
>>> b
{'c', 'a', 'b'}
>>> c = set(['abc'])
>>> c
{'abc'}
>>> d = set(('abc'))
>>> d
{'c', 'a', 'b'}
>>> ('abc')
'abc'
>>> ('abc',)
('abc',)
>>> e = set(('abc',))
>>> e
{'abc'}