Getting started with Python (12/18)
Section 12 Collection
Hello everyone, today I will talk about the last of the four data structures of Python: collections.
1、 set
A set is an unordered and non-repeating sequence. Usually, we use sets to eliminate duplicate elements, or to test the relationship between two sequences: intersection, union or difference?
feature:
(1) The set is organized by {} braces.
(2) The elements of the set are unique.
(3) The elements of the set are disordered.
2、 Create a collection
You can use curly braces {} or the set() function to create a set.
**Note: ** To create an empty set, you must use set() instead of {}, because {} is used to create an empty dictionary.
**Description: **
(1) You can use {} to directly define a set.
(2) If you try to define a set with repeated elements, python will be automatically eliminated.
(3) Please pay attention to the expression of an empty set.
**PS: **When using the set() function to create a collection, what you need to know is that it actually converts a sequence object into a collection. This sequence can be a string, a list, a tuple, or even a dictionary, and it will also Automatic deduplication.
3、 Access to collection elements
A set is an unordered and non-repeating sequence, so it cannot be accessed using a subscript index.
**Description: **
Except that you can't use the subscript index to access the elements in a collection, the elements in the collection are still unchangeable, so you can't perform so-called modification operations on the elements in the collection.
4、 Append new elements to the collection
We can use the set.add() or set.update() method to add new elements to a collection.
**Description: **
(1) The set.add() or set.update() methods can add new elements to the collection, and duplicate elements can be automatically removed when they are encountered.
(2) The set.add() method adds new elements to the whole of an object.
(3) The set.update() method is added in a sequence. Therefore, when it encounters a string sequence, it will split the string and then append it.
5、 Deletion or emptying of collection elements
Using the set.remove() or set.clear() method, you can delete the specified element of the collection or clear a collection.
Description
(1) set.remove() can delete the specified elements in the set, and only one element can be specified at a time.
(2) The set.clear() method will clear all elements in the collection at once.
6、 Set operations: and (&), or (|), not (^), subtraction
Description
(1) The result of the operation with (&) will produce an intersection, that is, exclude different elements and generate a new set.
(2) or (|) The result of the operation will produce a union, that is, merge two sets, remove duplicate elements, and generate a new set.
(3) The result of the non-(^) operation will produce a complement, that is, exclude the same elements and generate a new set.
(4) The subtraction operation produces a difference set, which removes the same elements as another set.
7、 Conversion between set and list (list) and tuple (tuple)
8、 Summary of collection operation functions or methods
summary
In this lesson, we learned the collection of Python. Collection is another important data structure of python. Because it is disordered and non-repetitive, it cannot rely on the so-called index for access, nor can it be modified. But it can be added and deleted. More importantly, it supports set operations to obtain the intersection, union, complement and difference of two sets.
Preview
So far, we have finished learning the main data types of python: numbers, strings, lists, tuples, dictionaries and sets. Using these data types, we can already solve some application problems through programming. Next, we will use python to create several application projects.
**Any questions, please leave a message. **
Recommended Posts