SoFunction
Updated on 2025-03-02

Learn Python from Lao Qi's list (4)

There are indeed many topics about list, and they are also very useful in programming.

Some readers may ask, if you want to generate a list, besides writing elements one by one, is there a way to let the computer generate a list by itself according to a certain rule?

If you raise this question, it fully shows that you are a "lazy person", but this is not a bad thing. The world has made progress because of the existence of "lazy person". "Lazy people" are actually not lazy.

Operation on list

range(start,stop) generates a number list

range(start, stop[, step]) is a built-in function.

To study the functions of some functions, especially built-in functions, it is recommended that the reader first understand the meaning of the built-in function name. Because in python, the name is not taken casually, but represents a certain meaning. Regarding the issue of naming, you can see the content of "The Knowledge of Naming" in this series: The Forever Powerful Function.

range
n. Range; amplitude; row; mountain range
vi. (within...) Change; parallel, listed as a row; extension; roaming; range reach
vt. roam; grazing; juxtaposing; categorizing; walking back and forth
Before the specific experiment, we should follow the management and excerpt the original words of an official document so that we can deeply understand:

Copy the codeThe code is as follows:

This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised).

From this passage, we can draw the following points about the range() function:

This function creates a list of numeric elements.
This function is most commonly used in for loops (about for loops, it will be involved soon)
The parameters of the function must be integers, and the default starts from 0. The return value is a list similar to [start, start + step, start + 2*step, ...].
The default step value is 1. If you don't write it, it's the value.
If step is a positive number, the last value returned to the list does not contain the stop value, that is, the value of start+astep is less than stop; if step is a negative number, the value of start+astep is greater than stop.
step cannot be equal to zero. If it is equal to zero, an error will be reported.
Before the experiment begins, explain the meaning of range(start,stop[,step]):

start: The value of the start is 0, which means that if you don't write this item, you think that start=0
stop: The end value must be written.
step: The step size of the change is 1 by default, which means that the step size is 1. Never be 0
To begin the experiment, please compare the previous description with each item:

Copy the codeThe code is as follows:

>>> range(9)                  #stop=9, nothing else is written, the meaning is range(0,9,1)
[0, 1, 2, 3, 4, 5, 6, 7, 8] # Starting from 0, step size is 1, increase until the number less than 9
>>> range(0,9)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
>>> range(0,9,1)
[0, 1, 2, 3, 4, 5, 6, 7, 8]

>>> range(1,9)              #start=1
[1, 2, 3, 4, 5, 6, 7, 8]

>>> range(0,9,2)            #step=2, each element is equal to start+i*step,
[0, 2, 4, 6, 8]

Just explain range(0,9,2)

If it starts from 0 and the step size is 1, you can write it as range(9), but if the step size is 2 and write it as range(9,2), the computer will be a little confused, and it will think that start=9, stop=2. Therefore, when the step size is not 1, do not write the value of start as well.
The first value in start=0, step=2, stop= is start=0, and the second value is start+1step=2 (note that it is 1, not 2, don’t forget, as mentioned earlier, whether it is list or str, when numbering elements, it start+(n-1)step). The nth value is start+(n-1)step. Until less than the value before stop.
Be familiar with the above calculation process and see who is the result of the following input?

>>> range(-9)
I originally expected to return [0,-1,-2,-3,-4,-5,-6,-7,-8] to me, can my expectations be realized?

Let’s analyze it, here start=0, step=1, stop=-9.

The first value is 0; the second is start+1*step. If you substitute the above number, it should be 1, but the last one is still -9, obviously there is a problem. However, python does not report an error here, and the result it returns is:

Copy the codeThe code is as follows:

>>> range(-9)
[]
>>> range(0,-9)
[]
>>> range(0)
[]

Reporting an error and returning a result are two meanings, although what we return is not what we want. How should it be modified?

Copy the codeThe code is as follows:

>>> range(0,-9,-1)
[0, -1, -2, -3, -4, -5, -6, -7, -8]
>>> range(0,-9,-2)
[0, -2, -4, -6, -8]

With this built-in function, many things are simple. for example:

Copy the codeThe code is as follows:

>>> range(0,100,2)
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

A list composed of even numbers in natural numbers within 100 is very simple to handle.

Thinking about a question, now there is a list, such as ["I","am","a","pythoner","I","am","learning","it","with","qiwsir"]. You need to get a list composed of all the serial numbers of this list, but you cannot count them one by one with your finger. what to do?

Please think for two minutes, experiment on your own, and then look at the following.

Copy the codeThe code is as follows:

>>> pythoner
['I', 'am', 'a', 'pythoner', 'I', 'am', 'learning', 'it', 'with', 'qiwsir']
>>> py_index = range(len(pythoner))     #The value of len(pythoner) is the stop
>>> py_index
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Then point your finger at the elements in pythoner and count it, is it the same as the result?

Sitting in a row, splitting fruits

Sorting is everywhere in reality and on the Internet. Liangshan heroes need to sort from the first to the first one, which is not easy to handle.

The result obtained by the built-in function range() mentioned above is a sorted order. How to sort a list that is not sorted well?

There are two ways to implement sorting lists:

(cmp=None, key=None, reverse=False)
sorted(iterable[, cmp[, key[, reverse]]])
Through the experiment below, you can understand how to sort

Copy the codeThe code is as follows:

>>> number = [1,4,6,2,9,7,3]
>>> ()
>>> number
[1, 2, 3, 4, 6, 7, 9]

>>> number = [1,4,6,2,9,7,3]
>>> number
[1, 4, 6, 2, 9, 7, 3]
>>> sorted(number)
[1, 2, 3, 4, 6, 7, 9]

>>> number = [1,4,6,2,9,7,3]
>>> number
[1, 4, 6, 2, 9, 7, 3]
>>> (reverse=True) #Start to implement reverse order
>>> number
[9, 7, 6, 4, 3, 2, 1]

>>> number = [1,4,6,2,9,7,3]
>>> number
[1, 4, 6, 2, 9, 7, 3]
>>> sorted(number,reverse=True)
[9, 7, 6, 4, 3, 2, 1]

In fact, in high-level languages, sorting is a relatively popular topic. If you are interested, you can check the topics about sorting in the algorithm I wrote.

At this point, the built-in functions for the basic operations of list are almost done. But in the end, I have to tell the audience a learning method. Because Python often has a lot of built-in functions, sometimes it is difficult to learn everything based on tutorials alone. So, the most important thing is to find out which functions are available. How to find it?

A very important method

Suppose there is a list, how do you know the built-in functions it has? Please use help() and help me.

>>> help(list)
You can see all the functions about list and how to use this function.