I want to learn more about Python-compatible list (4) and python.

Source: Internet
Author: User

I want to learn more about Python-compatible list (4) and python.

There are indeed many topics in list, and there are many uses in programming.

Some readers may ask, if you want to generate a list, in addition to writing the elements one by one, is there any way for the computer to generate a list according to a specific rule?

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

List operations

Range (start, stop) to generate a number list

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

To understand some functions, especially the built-in functions, we recommend that you first understand the meaning of the built-in function name. In python, the name is meaningful because it is not random. For more information about how to get a name, see the "Learning to get a name" section in this series: always powerful functions.

Range
N. range; amplitude; row; mountains
Vi. (In...) change; parallel, column as a row; Extend; roaming; range reached
Vt. Roaming; grazing; make parallel; classified as; move back and forth
Prior to the specific experiment, we still follow the management to extract the original words of an official document, which gives us a deep understanding:

Copy 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 section, we can get the following points about the range () function:

This function creates a list composed of numeric elements.
This function is most commonly used in the for loop (for the for loop, it will be involved soon)
The function parameter must be an integer. The default value starts from 0. The returned value is a list similar to [start, start + step, start + 2 * step.
The default value of step is 1. If this parameter is not specified, the value is used.
If step is a positive number, the final value of the returned list does not contain the stop value, that is, the value of start + istep is smaller than the stop value. If step is a negative number, the value of start + istep is greater than the stop value.
Step cannot be equal to zero. If it is equal to zero, an error is returned.
Before the experiment starts, explain the meaning of range (start, stop [, step:

Start: start value. The default value is 0. If this parameter is not specified, start = 0 is considered.
Stop: end value, which must be written.
Step: The changed step size. The default value is 1, that is, do not write, that is, the step size is 1. Cannot be 0
The experiment starts. Please compare the items described above:

Copy codeThe Code is as follows:
>>> Range (9) # stop = 9, nothing else, meaning range (, 1)
[0, 1, 2, 3, 4, 5, 6, 7, 8] # Start from 0, step 1, increase until the number smaller 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 (, 2) # step = 2. Each element is equal to start + I * step,
[0, 2, 4, 6, 8]

Just explain range (, 2)

If it starts from 0 and the step size is 1, it can be written as range (9). However, if the step size is 2 and it is written as range (9, 2), the computer will be a bit confused, it considers start = 9, stop = 2. Therefore, when the step size is not 1, do not write the start value.
Start = 0, step = 2, stop = 9. the first value in the list is start = 0, and the second value is start + 1 step = 2 (Note: Here is 1, not 2, don't forget, as mentioned earlier, whether it is list or str, it starts from 0 when the element is numbered. The Nth value is start + (n-1) step. Until the value is earlier than the value before stop.
I am familiar with the above calculation process and can see who is the result of the following input?

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

For analysis, start = 0, step = 1, stop =-9.

The first value is 0, and the second value is start + 1 * step. The number above should be 1, but the last one is-9, which obviously has a problem. However, python does not return an error here. The returned result is:

Copy codeThe Code is as follows:
>>> Range (-9)
[]
>>> Range (0,-9)
[]
>>> Range (0)
[]

The error message and the returned result have two meanings, although the returned result is not what we want. How should I modify it?

Copy 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 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 a natural number of less than 100 is very simple.

Think about a problem. Now there is a list, such as ["I", "am", "a", "pythoner", "I", "am", "learning ", "it", "with", "qiwsir"], to obtain a list consisting of all serial numbers of this list, you cannot count them one by one. What should I do?

Think twice and experiment on your own.

Copy codeThe Code is as follows:
>>> Pythoner
['I', 'am', 'A', 'pythoner', 'I', 'am', 'learn', 'it', 'with ', 'qiwsir ']
>>> Py_index = range (len (pythoner) # Use len (pythoner) as the stop Value
>>> Py_index
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Point to the element in pythoner with your finger and count it as a result.

Exclusive sit, fruit splitting

Sorting is everywhere in reality or on the network. Liang shanhaohan will sort the number from the first to 108th, which is not easy to handle.

The result of the built-in function range () mentioned above is a sorted order. How can I sort a list without sorting well?

There are two ways to sort the list:

List. sort (cmp = None, key = None, reverse = False)
Sorted (iterable [, cmp [, key [, reverse])
The following experiment shows how to sort data.

Copy codeThe Code is as follows:
>>> Number = [1, 4, 6, 2, 9, 7, 3]
>>> Number. sort ()
>>> 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]
>>> Number. sort (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, sorting is a hot topic in advanced languages. If you are interested, you can go to the related algorithms I wrote to view the topics related to sorting.

So far, the built-in functions for basic list operations are almost the same. But at last, we also need to tell our readers a learning method. Because python has many built-in functions, and sometimes it is difficult to learn all of them through tutorials, the most important thing is to find out which functions can be used by yourself. 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 () to help me.

>>> Help (list)
You can see all the list functions and how to use them.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.