With old Ziko Python's capacity is large list (4) _python

Source: Internet
Author: User
Tags in python

List is a lot of topics, and, in programming, there is a lot of use.

There are reader may ask, if you want to generate a list, in addition to the element of a write on the outside, there is no way to let the computer itself according to a certain law to generate a list of methods?

If you ask this question, you are a "lazy person", but this is not a bad thing, the world is because of "lazy man" the existence and progress. "Lazy person" is actually not lazy.

Action 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 the built-in functions, it is recommended that reader first understand the meaning of the built-in function names. Because in Python, the name is not random, it represents a certain meaning. On the question of naming, you can look at the reference in this series: the "Learn to name" section of the ever powerful function.

Range
N. extent; range; row; mountains
VI. (in ...) In the same line, as in a row;
Vt. To make a trip or a herd;
Before the specific experiment, or in accordance with the management, excerpt a piece of official documents, let us have a profound understanding of:

Copy Code code 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 was 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 is positive, the last element is the largest start + I * step less than stop; If is negative, the last element is the smallest start + I * step greater than stop. Step must isn't be zero (or else valueerror is raised).

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

This function can create a list of numeric elements.
This function is most commonly used in a For loop (about the For loop, which is immediately involved)
The parameter of the function must be an integer, and the default starts at 0. The return value is similar to [Start, start + step, start + 2*step, ...] The list.
The step default value is 1. If you do not write, you are following this value.
If step is a positive number, the last value that returns the list does not contain a stop value, that is, the value of Start+istep is less than stop, and if step is a negative number, the value of the start+istep is greater than the stop.
Step cannot equal zero, if equal to zero, then the error.
Before beginning the experiment, explain the meaning of range (Start,stop[,step]):

Start: The starting value, which defaults to 0, which means that if you don't write this, you think start=0
Stop: The value that must be written for the end.
Step: Change the STRIDE length, the default is 1, that is not write, that is, the step is 1. Determined not to be 0
To start the experiment, please refer to the preceding story:

Copy Code code as follows:

>>> Range (9) #stop = 9, nothing else is written, meaning is range (0,9,1)
[0, 1, 2, 3, 4, 5, 6, 7, 8] #从0开始, step is 1, increase, until less than 9 of that number
>>> 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 equals Start+i*step,
[0, 2, 4, 6, 8]

Just explain range (0,9,2)

If you start at 0, the step is 1, you can write a range (9), but if the step is 2, as a range (9,2), the computer is a bit confused, it will think start=9,stop=2. So, when the step is not 1, avoid, to the start of the value also write.
The first value in Start=0,step=2,stop=9.list is start=0, and the second value is start+1step=2 (Note that this is 1, not 2, and don't forget, as we've said before, whether it's list or str, the element is numbered, Are all starting from 0), the nth value is start+ (n-1) step. Until it is less than the value before the stop.
Familiar with the above calculation process, see the following input who is what results?

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

Analysis, here start=0,step=1,stop=-9.

The first value is 0; the second one is Start+1*step, the number of the above, should be 1, but the last one or-9, obviously there is a problem. However, Python does not complain here, and the result it returns is:

Copy Code code as follows:

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

The error and return result is two meanings, although the return is not what we want. How should it be modified?

Copy Code code 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, a lot of things are simple. Like what:

Copy Code code 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 , 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

A list of even numbers in the natural number within 100 is very simply done.

Think about a problem and now there's a list that for example, ["I", "AM", "a", "Pythoner", "I", "AM", "learning", "it", "with", "Qiwsir"], to get the list of all the serial numbers of this list, But you can't count one finger at a head. What to do?

After two minutes of meditation, experiment yourself and look below.

Copy Code code as follows:

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

Then point the finger at the element in the Pythoner and count it to be the same as the result.

Row sit, fruit

The sort, whether in reality or on the internet are everywhere. Liangshan heroes to be sorted from the first to the 108th, this is a not easy to handle the work.

The result of the built-in function range () mentioned earlier is a sorted order. How do you sort a list that doesn't have a good sequence?

There are two ways to sort the list:

List.sort (Cmp=none, Key=none, Reverse=false)
Sorted (iterable[, cmp[, key[, reverse]])
Through the following experiments, you can understand how to sort the method

Copy Code code 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) #开始实现倒序
>>> 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 the high-level language, ranking is a relatively hot topic, if interested readers, can go to my writing about the algorithm to see the topic about sorting.

So far, the built-in functions for the basic operations of the list are pretty much the same. But finally, tell the reader a way to learn. Because Python has a lot of built-in functions, and sometimes it's hard to learn everything by tutorial, it's critical to find out what functions you can use. How do I find it?

A very important method.

Suppose you have a list, how do you know what built-in functions it has? Help me, please.

>>> Help (list)
You will be able to see all of the functions about list and how to use the function.

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.