Getting started with Python (15/18)
Section 15****Application Cases (3)
Hello everyone! Yesterday’s case analysis, we have had an addiction, and today we concentrate on talking about a relatively complicated case of two-dimensional data sorting.
The so-called two-dimensional data sorting, I think everyone should have an impression of the sorting of Execl's data table, it can be sorted by any column. Such a data table is a typical two-dimensional data, which can realize fast arbitrary column sorting, which brings great convenience to our daily work. So if the sorting function of this table requires us to design its program code, what should we do?
In fact, for a set of two-dimensional data, the sorting of a single row or column of data is very simple, regardless of Python's built-in sorting function, at least you can use the bubble sort algorithm to get them.
However, the problem is complicated. The row and column data in the data table are not isolated.
Each column represents the measurement value of the same measurement category on different objects, we usually call it field and field value. Each row of the data table represents the same measurement object and the measurement values in different measurement categories. We usually call a row a record, which stores the measurement values of different measurement characteristics of the same measurement object.
After understanding the meaning of fields and records, our subsequent description is much simpler.
That is to say, the value of each field of the same record represents a certain characteristic of the record, then it cannot be moved randomly, if a certain value of a column is suddenly caused by the sorting of this column The line breaks, does it still identify a certain characteristic of the original record? Obviously not, because it is no longer in that line, it has become a feature of other people's homes.
There is only one possibility to maintain the ideographic characteristics of the original data table (ie, two-dimensional data) without causing data confusion. That is, if the data of a certain column of a record changes position due to sorting, then , The position of the other columns of this record should also be changed synchronously, that is, it is migrated together in the entire row, so that not only the data record of this row is still complete, but also will not be affected by its changes For data records of other rows, all data belonging to the same row are still in the same row and belong to the same record (without going to someone else's home).
Therefore, we may have discovered the way to solve a similar problem (two-dimensional data sorting problem), that is, the sorting of a certain column, not the sorting of isolated columns, but the synchronous migration of the position of the entire row of data records.
Well, so far, we finally have the courage to face today's case.
Case 6. A set of two-dimensional data can be sorted by any column
**Requirements: **Assuming the data is as follows:
1,' a',2.35
5,' d',7.22
0,' c',8.99
3,' b',5.33
2,' e',6.99
Please sort by any column.
demand analysis
(1) There are 5 rows and 3 columns of data. The data in the first column is an integer, the data in the second column is a string, and the data in the third column is a decimal. We can use a two-dimensional list to represent this set of two-dimensional data. A row of two-dimensional data constitutes an object (element) of the list. Therefore, the list will have 5 objects (elements).
(2) What we want to sort is not rows, but columns. Because, under normal circumstances, we will record the same type of characteristic values in a column, so they are comparable. Of course, if you don't consider the practical significance and comparability, you must sort by rows. The logic of the algorithm should be the same. In fact, we will do it soon.
(3) In order to be able to sort the data in the same column of different rows (compare the size), we need to first perform a row-column conversion on the data in this list, so that it becomes a two-dimensional data with three rows and five columns. Please note that this conversion has no other purpose. It is purely to make the data of each column become an independent pair of elements in the list to facilitate the sorting operation. The important thing is that this conversion does not change the correspondence between the data at all.
(4) If the sorting goes well, let's do another inverse transformation (row and column transformation again). Can you imagine anything? Isn't that the final sorting result we want?
(5) So, the question finally focuses on how to sort the data of a new row after the rank conversion. For the same reason, in two-dimensional data, the sorting of a row should also be the problem of the position of the related column moving forward? OK, after understanding this, things really become easier. Because the sorting of a list object itself is only a matter of sorting function, it can be done with one trick.
**Well, having said so much, it's probably a bit brain-burning, I'm really sorry. However, we must know that the brain is a good thing, and everyone does not need it, but if it works well, the difference will be big. It should be known that the more you burn, the more spiritual, why not burn it? I burn my glory! **
The source code for sorting two-dimensional data according to any column is given below.
PS: In order to facilitate the understanding of beginners, we have made line-by-line comments, which is purely for the convenience of novices. Please note that under normal circumstances, line-by-line comments are a bit scolding and thankless. Why is that? Because people who read the code may feel that you think of others' "big cows" as "xiaobai", you can make comments at key positions. What do you mean by line-by-line comments? Can't I understand a single line of code? You are awesome, are we all idiots? Grass...Who doesn't have a sense of pride or superiority? Look, look, unprovoked kindness is treated as a donkey's liver and lungs, thankless, and offending people, so, the world is sinister, brother! Everything can be done in moderation, too late, the most reasonable sayings, the most reasonable sayings.
**Code analysis: **
1、 Any two-dimensional data can be defined as a two-dimensional list, which is actually a nested list, which means that each element of the list is still a list object. However, please note that not every list containing nested lists is a two-dimensional data. Because, whether each nested list it contains has the same data type and list length is the key to determining whether it can be regarded as a two-dimensional data.
2、 When we understand the sorting of columns, in order to ensure that the data structure remains unchanged, the sorting of the columns can actually be understood as the adjustment of the order of the rows.
3、 Realizing the conversion of ranks and columns is a key point in operation. From this we thought of the rank conversion function of the zip() function. After the rank conversion of the list data with the zip(*list) function, a list of tuples in the unit of column will be generated, and then it is convenient to sort Much.
4、 The main point is that after sorting according to the specified column elements, the corresponding other columns must also undergo synchronization position migration, to be more precise, the synchronized index changes. Please pay attention to the details of the code implementation: after the row and column conversion, each original column becomes a tuple. In this way, other columns only need to refer to the index change after the specified column is sorted and perform the same index change. Realize the synchronous update of all columns.
5、 Finally, using the zip() function to restore, the two-dimensional data is sorted by the specified column.
summary
As of today's sharing, we have a total of 6 Python application design cases. This gave us a preliminary taste of the beauty of python programming, simple and elegant!
So far, we have shared the most basic and commonly used knowledge points of python.
It should be emphasized that there is no unique solution for any requirement. You can try to find different solutions and give us feedback in the message area. In order to consolidate the learning achievements, it is recommended that you can find more design topics after class, use your brain and hands more, the iron rod will be ground into a needle, and your work will come naturally!
Preview
Next, we are going to share the python object-oriented programming, which will be very, very important. Having learned the thinking ability and methods of object-oriented programming, there is no doubt that it will not only further improve our python programming level, but also let us take another big step towards the realm of masters.
Recommended Posts