Getting started with Python (9/18)
Section 9****Data Structure: List
Hello everyone, after we learn the python modules, we can almost write complete Python applications, and even in the face of some relatively complex application requirements, we can also build a beautiful system architecture through packages and modules.
However, what if the so-called complex application requirements we encounter are not only complex in business logic, but complex in data structure? The so-called complexity of data structure, for example, the amount of data is not only large, but also different types and characteristics need to be treated differently. So, our understanding of data is far from enough based on concepts such as integers, decimals, and variables. In fact, the problems we want to solve in programming are often not purely textual. Many non-digital objects happen to be the objects we really want to deal with. Moreover, according to the characteristics of the research object, we can summarize some data with the same characteristics. If we group them together and perform similar processing, the efficiency should be much higher. This is a basic idea.
Based on this idea, starting today, we need to further study more complex data types, which are the so-called four data structures of Python.
Before we start, we still need to spend a little time to build a very important understanding. In the process of actually dealing with problems, we will always face a large amount of data, and we can even understand that in the real world , Solving the problem itself is processing data. Without the data, any program method will be "nothing to cook".
It is easy to find that almost every computer language emphasizes that everything is an object. However, to solve problems, data processing is ultimately necessary. Therefore, behind everything is an object, it actually means that everything is data. Because, with the data, you can solve the corresponding problems by establishing a suitable model. Therefore, how to understand objects and data and establish corresponding solutions to problems is the core thinking ability we programmers need to have.
Academically, it is called data structure and algorithm.
Have you noticed that in all the application examples we listed above, the reason why we use some expressions and flow control statements, even functions, modules and other technologies, is actually to deal with some variables, including operations through assignments and operations , And finally realize data processing and return the results we want.
The truth of programming is that good-looking code flows are just means, and data processing is the goal.
Among them, variables are the most basic data, and the method of processing data is the so-called algorithm. Algorithms are like wizards, ever-changing, and rely on our wisdom. But data is a real and objective existence. How to properly transform real-world objects into data that we can handle conveniently is what we need to practice! So, in today's course, we began to focus on the data structure of Python.
Data Structures is as its name. From the perspective of Python syntax, it is just a structure. We can visually understand that it is a box that can store data. Different data structures represent different types of boxes. To be precise, they are used to store a collection of related data. There are three meanings in this description:
1、 The keyword "a series" means that the data generally does not exist in isolation.
2、 The keyword "related" means data that can be stored together and is relevant.
3、 The keyword "set" borrows the concept of mathematics to indicate that the way data exists is operable, such as adding, modifying, deleting, querying, sorting, merging... and so on.
Let's take a look at what kind of data structure Python has?
In addition to the ubiquitous constants and variables as the most basic data types, there are four built-in data structures in Python. It will become a weapon for us to realize object dataization and data processing. Only by using them proficiently can you truly realize the beauty of it!
Due to space limitations, we will introduce these four data structures separately in four lessons. Today, focus on the list (list)
1、 List
Python's list (list) is a flexible collection of ordered objects. **We mentioned earlier that a string is a sequence, and a list is obviously a sequence. However, lists and strings are different. **
1、 Lists can contain any kind of objects, and can even be nested. A list can contain another list as one of the objects.
2、 The list contains mutable objects and supports real-time modification (modification in place).
3、 The list can be increased or decreased as needed.
4、 With the help of lists, we can almost create and process arbitrarily complex data information through scripts.
**Therefore, the list is extremely important and the application is extremely common. **
2、 Create a list
Putting the comma-separated objects in a pair of square brackets creates a list.
**Explanation: **When a list is assigned to a variable name, then we will use this variable as the name of the list. (Essentially a reference is created between the list object and the variable name), please pay attention to how to define an empty list and a single item list.
3、 Access the value in the list
Since the list is a sequence, the data of the sequence type has an index and an index corresponding value. Therefore, we can use the subscript index to access the values in the list.
The following example: list_1.py.
Please note that in the above example, we used the concept of the so-called slice operation. Here, add an explanation:
(1) The slicing operation is a kind of segmentation operation specifically for sequence type data.
(2) A slice is a segment of sequence data.
(3) The slicing method, take list data as an example: list[a:b:c], it will get a slice, this slice starts from the index position of a of the list data, it can be omitted when it is 0; it ends before b , Can be omitted if it goes directly to the end; c is the slice direction, the default positive direction, can be omitted.
(4) To fully understand slices, we also need to know that there is an infrequently used index, that is, the reverse index, which defaults to -1 from the end of the sequence.
4、 update list
We can use reassignment, append() method, del statement to modify or update the data items of the list.
1、 Through the index, directly re-assign the value of the specified subscript.
2、 Use the append() method to append new items to the end of the list.
3、 Use the del statement to delete the item with the specified subscript by index.
5、 List operators
The operators of the list pairs + and * are similar to strings.
PS: Although almost all sequences (including strings) support the merge operation of the + sign, the plus sign + must be two sequences of the same type. You cannot add a list and a string together!
6、 Functions for list operations
(1), len(list): Returns the number of list elements.
(2), max(list): returns the maximum value of list elements.
(3), min(list): return the minimum value of list elements
(4), list(seq): Convert tuples to lists, the next section will introduce tuples.
(5), range(start, stop[, step]): Create a list of integers, generally used in a for loop.
7、 List operation method
(1), list.append(obj): add a new object at the end of the list.
(2), list.count(obj): Count the number of times an element appears in the list.
(3), list.index(obj): Find the index position of the first matching item of a certain value from the list.
(4), list.insert(index, obj): insert the object into the list (in front of the specified index position).
(5), list.pop(obj=list[-1]): Remove an element from the list (the last element by default), and return the value of the element.
(6), list.remove(obj): remove the first match of a value in the list.
(7), list.reverse(): Reverse arrangement of the original list.
(8), list.sort([func]): sort the original list.
(9), list.extend(seq): append another sequence value at the end of the list.
8、 Merging and appending lists
1、 The merge operation of the list list uses the "+" sign, which will generate a new list.
2、 append(obj): Add a new object at the end of the list, and use the new object to extend the original list.
3、 extend(seq): Append the value in another sequence to the end of the list, and extend the original list with the new list.
4、 Please note that in the last example below, the string is appended as a sequence rather than as a whole.
summary
In this section, we began to get in-depth contact and understanding of Python's data structure, and focused on the list and the method of creating and using it.
Preview
In the next lesson, we continue to introduce the Python data structure: tuples, which are also one of the most commonly used data structures in Python. Obviously, it has similarities and differences with the list. In terms of data processing, it has a completely different operating charm.
Recommended Posts