Chapter 0 Preschool Preparation
Chapter 1 Data Structure – Basic Data Types
Chapter 1 Data Structure – String< a i=3>Chapter 1 Data Structure – List, Tuple and SliceChapter 1 Data Structure – DictionaryChapter 1 Data Structure – Set a>
Article directory
Chapter 1 Data Structure – Collection
1.2 Combined data types
1.2.6 Collection
The collection has two implementation classes: mutable set
and immutable frozenset
.
The essence of a collection is an aggregation of many unique objects. Therefore, the collection has the function of deduplication. The elements in the collection must be hashable, the set
type itself is not hashable, but frozenset
is.
1.2.6.1 Definition and initialization
# 1", "空集
from unicodedata import name
test_set = set()
print(test_set)
# 2", "集合的字面量
test_set = {
1, 2, 3}
print(test_set)
# 3", "通过可迭代对象
test_set = set([1, 2, 3, 4])
print(test_set)
# 4", "集合推导setcomps
{
chr(i) for i in range(32, 256) if 'SIGN' in name(chr(i), '')}
1.2.6.2 Collection operations
1.2.6.2.1 Mathematical operations on sets
Mathematics Symbol | Python operators | method | describe |
---|---|---|---|
S ∩ Z | s & z | s.and(z) | The intersection of s and z |
z & s | s.rand(z) | Reverse & operation | |
s.intersection(it , ...) | Convert the iterable it and all other parameters into sets, and then find their intersection with s | ||
s &= z | s.iand_(z) | Update s to the intersection of s and z | |
s.intersection_update(it , ...) | Convert the iterable it and all other parameters into a set, then find their intersection with s, and then update s to this intersection | ||
S ∪ Z | s | z | s.or(z) | Union of s and z |
z | s | s.ror(z) | The reverse operation of | | |
s.union(it , ...) | Convert the iterable it and all other parameters into sets, and then find their union with s | ||
s |= z | s.ior_(z) | Update s to the union of s and z | |
s.update(it , ...) | Convert the iterable it and all other parameters into a set, then find their union with s, and then update s to this union | ||
S \ Z | s - z | s.sub(z) | The difference set of s and z, or the relative complement set |
z - s | s.rsub(z) | - the reverse operation of | |
s.difference(it , ...) | Convert the iterable it and all other parameters into sets, and then find the difference between them and s | ||
s -= z | s.isub_(z) | Update s to the difference between s and z | |
s.difference_update(it , ...) | Convert the iterable it and all other parameters into a set, then find their difference with s, and then update s to this difference set | ||
s.symmetric_difference(it) | Find the symmetric difference between s and set(it) | ||
S △ Z | s ^ z | s.xor(z) | Symmetric difference set of s and z |
z ^ s | s.rxor(z) | Reverse operation of ^ | |
s.symmetric_difference_update(it , ...) | Convert the iterable it and all other parameters into sets, then find the symmetric difference between them and s, and finally update s to the result | ||
s ^= z | s.ixor_(z) | Update s to the symmetric difference set of s and z |
1.2.6.2.2 Comparison operators for sets
Mathematics Symbol | Python operators | method | describe |
---|---|---|---|
s.isdisjoint(z) | Check if s and z are disjoint (have no common elements) | ||
e ∈ s | e in s | s.contains(e) | Whether element e belongs to s |
e ∈ s | e in s | s.contains(e) | Whether element e belongs to s |
s ⊆ z | s <= z | s.le(z) | Whether s is a subset of z |
s.issubset(it) | Convert the iterable it into a set, and then check whether s is a subset of it | ||
s ⊂ z | s < z | s.lt(z) | Whether s is a proper subset of z |
s ⊇ z | s >= z | s.ge(z) | Whether s is the parent set of z |
s.superset(it) | Convert the iterable it into a set, and then check whether s is its parent set | ||
s ⊃ z | s > z | s.gt(z) | Whether s is the true parent set of z |
1.2.6.2.3 Other methods of collection types.
set
is sorted out of order and does not repeat. It is variable. There are add()
, remove()
and other methods. Since it is mutable, it does not have a hash value. Basic functionality includes relationship testing and elimination of duplicate elements. Collection objects also support union
(union), intersection
(intersection), difference
( Mathematical operations such as difference set) andsysmmetric difference
(symmetric difference set). set
supports x in set
, len(set)
, and for x in set
.
As an unordered set, set
does not record element positions or insertion points. Therefore, set
does not support indexing
, or other sequence-like operations. frozenset
is a frozen collection. It is immutable and has a hash value. The advantage is that it can be used as a dictionary key
or as an element of other collections. The disadvantage is that once created, it cannot be changed, and there is no add
, remove
method.
Python methods | set | froznset | describe |
---|---|---|---|
s.add(e) | * | Add element e to s | |
s.clear() | * | Remove all elements in s | |
s.copy() | * | * | Shallow copy of s |
s.discard(e) | * | If there is an element e in s, remove it | |
s.__iter__() | * | * | Returns an iterator over s |
s.__len__() | * | * | only |
s.pop() | * | * | Removes an element from s and returns its value. If s is empty, a KeyError exception is thrown. |
s.remove(e) | * | Remove the e element from s. If the e element does not exist, throw a KeyError exception. |