1.1 collection
It is an iterable, unordered data structure that cannot contain repeated elements. The elements of the collection are immutable, such as: int, float, string, tuple, etc. The variable content cannot be the elements of the collection, such as: list, dict, set, etc.
Collections can be divided into:
- Mutable collection (set)
- Immutable collections (frozenset)
1.2 Mutable collections
1.2.1 Create collection
>>> a = {1,3,2,4} >>> a {1, 2, 3, 4}
It is represented by big {}, note: the dictionary is also represented by {}, but it stores key-value pairs.
>>> a = set() # create an empty collection, if a={} is an empty dictionary >>> a set()
Use the set function to create a collection, which is followed by a sequence:
>>> a = set("hello") >>> a {'e', 'o', 'h', 'l'}
1.2.2 Modifying mutable collections
-
Add element: add(elem)
Note: Duplicate elements cannot be added because collection elements are not repeated.
>>> a = set() >>> a = {1,2,3,4} >>> a.add(5) >>> a {1, 2, 3, 4, 5} >>> a.add(4) >>> a {1, 2, 3, 4, 5}
-
Delete elements: remove(elem); discard(elem)
The difference between the two elements is that an exception is thrown before deleting an element that does not exist, and the latter will not.
>>> a = {1,2,3,4} >>> a.remove(1) >>> a {2, 3, 4} >>> a.discard(4) >>> a {2, 3} >>> a.remove(4) # 4 does not exist, an exception is thrown Traceback (most recent call last): File "<stdin>", line 1, in <module> KeyError: 4
- Delete any element, the return value is the deleted element: pop()
>>> a = {1,3,2,4} >>> a.pop() 1
- Empty collection: clear()
>>> a = {1,2,3,4} >>> a.clear() >>> a set()
1.2.3 Other methods
- Copy a collection: copy()
>>> a = {1,2,3,4} >>> b = a.copy() >>> b {1, 2, 3, 4}
- The empty intersection of two collections returns true: isdisjoint()
>>> a = {1,2,3,4} >>> b = {1,2} >>> a.isdisjoint(b) # Intersect returns False False
- is a subset returns true: issubset()
>>> a = {1,2,3,4} >>> b = {1,2,3,4,5} >>> a.issubset(b) True >>> b.issubset(a) False
- is a superset returns true: issuperset()
>>> a = {1,2,3,4,5} >>> b = {1,2} >>> c = {1,2,6} >>> a.issuperset(b) True >>> a.issuperset(c) False
- Merge collection to collection A: update()
>>> a = {1,2,3,4} >>> a.update({3,4,5,6}) >>> a {1, 2, 3, 4, 5, 6}
1.2.4 Operations on Sets
operator | illustrate |
---|---|
& | Intersection: Get elements that exist in both sets |
| | Union: Two collection elements are merged together, and duplicates are removed. |
- | Difference set: A set that belongs to set A but does not belong to set B |
^ | Symmetric difference set: only belongs to set A and set B, that is, what is left after removing the intersection. |
== | equal |
!= | not equal |
in | inside |
not in | not in |
- Intersection: intersection; intersection_update
>>> a = {1,2,3,4} >>> b = {1,2} >>> a & b {1, 2} >>> a.intersection(b) {1, 2} # intersection_update will modify the collection A, while the former generates a new collection >>> a = {1,2,3,4} >>> b = {3,4,5,6} >>> a.intersection_update(b) >>> a {3, 4}
- Union: union
>>> a = {1,2,3,4} >>> b = {5,6} >>> a | b {1, 2, 3, 4, 5, 6} >>> a.union(b) {1, 2, 3, 4, 5, 6}
- Difference set: difference; difference_update()
>>> a = {1,2,3,4} >>> b = {1,2} >>> a - b {3, 4} >>> a.difference() {1, 2, 3, 4} >>> b - a # Conversely subtract, there is no element after removal. set() # difference_update will modify collection A >>> a = {1,2,3,4} >>> b = {3,4,5,6} >>> a - b {1, 2} >>> a # no change {1, 2, 3, 4} >>> a.difference_update(b) >>> a # changed {1, 2}
- Symmetric difference: symmetric_difference(); symmetric_difference_update()
>>> a = {1,2,3,4} >>> b = {3,4,5,6} >>> a ^ b {1, 2, 5, 6} >>> a.symmetric_difference(b) {1, 2, 5, 6} # symmetric_difference_update modifies set A
1.2.5 Universal functions
function | illustrate |
---|---|
enumerate() | Number the elements and return an enumerate object |
len() | length |
max() | maximum value |
min() | minimum value |
sorted() | Sort, generate a new list |
sum() | to sum |
>>> a = {'a','b','c','hello'} >>> a {'a', 'b', 'hello', 'c'} >>> for i in enumerate(a): ... print(i) ... (0, 'a') (1, 'b') (2, 'hello') (3, 'c')
1.3 Immutable collection frozenset
Known as a frozen collection, it cannot be changed, but it can be accessed and set operations.
Immutable collections can be used as keys in dictionaries and as elements in collections.
# create >>> a = frozenset(["Zhang San","Li Si","hello",1,2,3]) >>> a frozenset({1, 2, 3, 'hello', 'Zhang San', 'Li Si'}) >>> type(a) <class 'frozenset'>
1.4 Set comprehensions
Same as list comprehension
# [item for item in iterable] # [item for item in iterable if condition] >>> a = {i for i in range(10)} >>> a {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}