Basic operations of Python collection
- 2021-12-12 04:44:34
- OfStack
1. Collection
Python
There is basically no difference between the set in and the mathematical set, which is out of order, that is, it cannot be accessed by index, and duplicate elements cannot appear in the set.
2. Create a collection
In
Python
Use curly braces to create collections in
{}
Literally, or using
set()
To create 1 collection. Use
{}
Must contain at least 1 element in, creating an empty collection cannot be used
{}
This creates an empty dictionary and should use the
set()
To create 1 collection.
# Create a collection using the literal method
set1 = {"Hello", "World"}
print(type(set1), set1) # <class 'set'> {'World', 'Hello'}
# print(set1[1]) # TypeError: 'set' object does not support indexing
# Use set() The way to create
set2 = set("hello") # {'l', 'o', 'h', 'e'} Collection removes duplicate elements, that is, "l"
print(set2)
# Create an empty collection
set3 = {}
set4 = set()
print(type(set3), type(set4)) # <class 'dict'> Dictionary <class 'set'>
list1 = [1, 2, 3, 4, 3, 5, 1, 3]
# Convert a list to a collection
set6 = set(list1)
print(type(set6), set6) # <class 'set'> {1, 2, 3, 4, 5}
# Using Build Columns to Build Lists
set5 = {num for num in range(20) if num % 3 == 0}
print(set5) # {0, 3, 6, 9, 12, 15, 18}
# Traversal loop
for ch in set5:
print(ch)
Note: The elements in the collection are immutable types, such as integers, floating points, strings, tuples, etc. That is to say, mutable types cannot be elements of tuples, and the collection itself is also mutable, so the collection cannot be elements in the collection.
3. Operation of sets
Set data types have many operators, including member operation, intersection operation, union operation, difference set operation, comparison operation (equality, subset, superset) and so on.
3.1 Member Operations
You can use member operations
in
And
not in
Check whether the element is in the collection,
The sample code is as follows:
set1 = {" How do you do ", "Python", " This is ", " Set ", "set"}
print("Python" in set1) # True
print(" How do you do " in set1) # False
print("set" not in set1) # False
print("list" not in set1) # True
3.2 Operation of intersection, union and difference
Python
The set in is similar to the set 1 in mathematics, which can be operated by intersection, union, difference set, etc., and can be operated by operator and method call.
The sample code is as follows:
set1 = {1, 2, 3, 4, 5, 6, 7}
set2 = {2, 4, 6, 8, 10}
# Intersection ( We both have it )
# Method 1: Use & Operator
print(set1 & set2) # {2, 4, 6}
# Method 2: Use intersection Method
print(set1.intersection(set2)) # {2, 4, 6}
# Union set (we add it to 1 From)
# Method 1: Use | Operator
print(set1 | set2) # {1, 2, 3, 4, 5, 6, 7, 8, 10}
# Method 2: Use union Method
print(set1.union(set2)) # {1, 2, 3, 4, 5, 6, 7, 8, 10}
# Difference set (I don't want what you have)
# Method 1: Use - Operator
print(set1 - set2) # {1, 3, 5, 7}
# Method 2: Use difference Method
print(set1.difference(set2)) # {1, 3, 5, 7}
# Symmetry difference (we don't want yours and mine, just special 1 Of)
# Method 1: Use ^ Operator
print(set1 ^ set2) # {1, 3, 5, 7, 8, 10}
# Method 2: Use symmetric_difference Method
print(set1.symmetric_difference(set2)) # {1, 3, 5, 7, 8, 10}
# Method 3: Symmetric difference is equivalent to the union of two sets minus the intersection
print((set1 | set2) - (set1 & set2)) # {1, 3, 5, 7, 8, 10}
The intersection, union and difference sets of sets can also be combined with the assignment operation 1 to form a compound operation.
The sample code is as follows:
set1 = {1, 3, 5, 7}
set2 = {2, 4, 6}
# Will set1 And set2 Find union and assign it to set1
# It can also be passed through set1.update(set2) To realize
set1 |= set2 # set1 = set1 | set2
print(set1) # {1, 2, 3, 4, 5, 6, 7}
set3 = {3, 6, 9}
# Will set1 And set3 Find the intersection and assign it to set1
# It can also be passed through set1.intersection_update(set3) To realize
set1 &= set3 # set1 = set1 & set3
print(set1) # {3, 6}
3.3 Comparison Operation
Two sets can be used with = = and! = Equality judgment, if the elements in two sets are identical, then
Python
0
The result of the comparison is that
True
Otherwise, it is False;; ! = vice versa.
If all elements of the set A are also elements of the set B, it is said that A is a subset of B and B is a superset of A; If the set A is a child and is not equal to the set B, it is said that A is a true child of B. The operators for determining subsets and supersets are < And > . Sample code ↓
set1 = {1, 3, 5}
set2 = {1, 2, 3, 4, 5}
set3 = set2
# < Operator represents a true subset, <= Operator represents a subset
print(set1 < set2, set1 <= set2) # True True
print(set2 < set3, set2 <= set3) # False True
# Pass issubset The method can also judge subsets
print(set1.issubset(set2)) # True
print(set2.issubset(set2)) # True
# issubset Methods and <= Operators are equivalent
# In turn, you can use issuperset Or > Operator for superset judgment
print(set2.issuperset(set1)) # True
print(set2 > set1) # True
4. The method of collection
Collection is of mutable column type, and the elements of the collection can be modified by methods of collection type.
Sample code:
# Create 1 Empty set
set1 = set()
# Pass add() Method to add elements to the collection
set1.add(1)
set1.add(2)
print(set1) # {1, 2}
# Pass update() Add elements to a collection
set1.update({1, 2, 3, 4, 5})
print(set1) # {1, 2, 3, 4, 5} Element already exists in the collection, the element will only appear 1 Times
# Pass discard() Method to delete the specified element
set1.discard(4)
set1.discard(2)
set1.discard(22) # There is no error in the collection
print(set1) # {1, 3, 5}
# Pass remove() Method to delete the specified element
# set1.remove(6) # KeyError: 6, Use remove() Method deletes the specified element without making an error by doing the member operation first
if 6 in set1:
set1.remove(6)
# pop Method can be randomly deleted from the collection 1 Elements and returns the element
print(set1.pop()) # 1 What is excluded this time is 1
# clear Method to empty the entire collection
set1.clear()
print(type(set1)) # <class 'set'>
print(set1) # set()
# Pass isdisjoint() Method determines whether two collections have duplicate elements, and returns False Returns if there is no Ture
set2 = {"Hello", "World"}
set3 = {"Hello", "1 Bowl week "}
set4 = {"Hi", "Python"}
print(set2.isdisjoint(set3)) # False
print(set2.isdisjoint(set4)) # True
5. Immutable sets
Python
There is also a collection of immutable types in the
frozenset
The difference between a set and a tuple is the same as the difference between a list and a tuple. In addition to the inability to add and delete elements,
frozenset
In other ways with
set
It's basically one.
Sample code:
set1 = frozenset({1, 3, 5, 7})
set2 = frozenset(range(1, 6))
print(set1 & set2) # frozenset({1, 3, 5})
print(set1 | set2) # frozenset({1, 2, 3, 4, 5, 7})
print(set1 - set2) # frozenset({7})
print(set1 < set2) # False