1 Introduction
In Python, the collection Set is one of the commonly used data types. This article summarizes the common functions and operations related to the collection, so that everyone can check for omissions and fill in the gaps.
Without further ado, let's get started!
2. Related concepts
2.1 Definition
A set set
is an unordered set with unique elements. It is similar to a list, but it can only contain unique elements. At the same time, it should be noted that the set is unordered, that is to say, there is no first or second element in the set. Usually we just say whether the set contains these elements.
2.2 Purpose
We usually need to detect whether an element belongs to a certain set, for example: suppose we have a list list1 = [3,4,5,6]
, if we need to check whether the list contains numbers 6
, we usually implement it like this:
print(6 in [3,4,5,6])
list
If we operate on lists in
, at this point Python will go through the entire list element by element until it encounters a number in the list 6
and return True
. If our list contains n
elements, then this operation will search n
times in the worst case.
What if we switch to a collection implementation? As follows:
print(6 in {
3,4,5,6})
We set
find an element in the collection is very fast, if the number of elements is large. n
Assuming there is an element in our collection , in
the operation on the collection only needs to be searched once, which is extremely fast compared to the list search.
3. Create a collection
We can use set
the constructor to create an empty collection as follows:
s1 = set()
Of course, we can also create a collection containing initial values, as follows:
s1 = {
4, 5, 6}
We usually use curly braces {
and }
to create a collection containing multiple elements. The difference between collections and dictionaries is that dictionaries require us to type :
key-value pairs separated by colons, while collections do not.
Note: We can only put immutable elements into collections. If we try to place mutable elements, such as lists or dictionaries, our program will report an error.
4. Add new elements to the collection
We generally use add
methods to add new elements to the collection, as follows:
s1 = {
3,4,5}
s1.add(6)
# s1 will be {
3,4,5,6}
It is important to note that if we try to add an already existing element to the set, nothing will happen because a set can only contain unique elements. A sample is as follows:
s1 = {
3,4,5}
s1.add(3)
# s1 will still be {
3,4,5}
5. Judging that the set contains an element
in
Operators are generally used to determine whether a collection contains an element. The sample code is as follows:
if 6 in {
3,4,5,6}:
print("6 is inside our set")
else:
print("not inside")
6. Calculate the length of the set
The length of a collection usually refers to the number of elements contained in the collection. We can use the built-in len
function to calculate. The sample code is as follows:
s1 = {
3,4,5,6}
x = len(s1)
# x will be 4, as there are 4 elements inside s1
7. Remove an element from a collection
If we need to remove an element from a collection, we can simply use .remove
the method to do so, as follows:
s1 = {
3,4,5,6}
s1.remove(6)
# s1 will now be {
3,4,5}
At this point, if the element we delete does not exist in the collection, an error message will be triggered, as follows:
s1 = {
3,4,5,6}
s1.remove(7) # error
Some students may say that we don't want to trigger an error when deleting, so we can simply use discard
the function, which remove
has the same function as the function, and at the same time, it will not trigger an error when deleting an element that does not exist. As follows:
s1 = {
3,4,5,6}
s1.discard(6) # removes 6
s1.discard(7) # removes nothing, but no error raised
8. Calculate the union of two sets
Suppose we have the following two collections:
s1 = {
1,2,3,4}
s2 = {
3,4,5,6}
We want to calculate the union of two sets, that is, to find the set of all elements in the above two sets. At this time, we can use the union
function. The example is as follows:
s3 = s1.union(s2)
# s3 will be {
1,2,3,4,5,6}
The diagram is as follows:
9. Calculate the intersection of two sets
Suppose we have the following two collections:
s1 = {
1,2,3,4}
s2 = {
3,4,5,6}
We want to calculate the intersection of two sets, that is, to find the set formed by the common elements in the above two sets. At this time, we can use the intersection
function. The example is as follows:
s3 = s1.intersection(s2)
# s3 will be {
3,4}
The diagram is as follows:
10. Calculate the difference of two sets
Suppose we have the following two collections:
s1 = {
1,2,3,4}
s2 = {
3,4,5,6}
We want to calculate the difference set of two sets, that is, find the set composed of elements that are in the set s1
and not in the set at the same time . At this time, we can use operators to obtain the difference set of the above two sets. Examples are as follows:s2
-
s3 = s1 - s2
# s3 will be {
1, 2}
Correspondingly, if we want to find elements that are in the collection s2
but not in the collection , we can do this:s1
s3 = s2 - s1
# s3 will be {
5, 6}
The diagram is as follows:
11. Summary
This article focuses on set
the common operations of collections in Python and the corresponding application scenarios, and gives relevant code examples.
Are you useless?