The range () function is detailed in Python

Source: Internet
Author: User

People who use Python know that the range () function is handy, and when you use him today, you find a lot of details that you've seen but forgotten. Here's a record of range (), review the list's slide, and finally analyze a fun bubbling program.

Here's a record:

>>> range(1,5) #代表从1到5(不包含5)[1, 2, 3, 4]>>> range(1,5,2) #代表从1到5,间隔2(不包含5)[1, 3]>>> range(5) #代表从0到5(不包含5)[0, 1, 2, 3, 4]

And look at the list operation:

array =[1, 2, 5, 3, 6, 8, 4]#其实这里的顺序标识是[1, 2, 5, 3, 6, 8, 4](0123456)(-7,-6,-5,-4,-3,-2,-1)>>> array[0:] #列出0以后的[1, 2, 5, 3, 6, 8, 4]>>> array[1:] #列出1以后的[2, 5, 3, 6, 8, 4]>>> array[:-1] #列出-1之前的[1, 2, 5, 3, 6, 8]>>> array[3:-3] #列出3到-3之间的[3]

Then two [::] What would it be?

>>> array[::2][1, 5, 6, 4]>>> array[2::][5, 3, 6, 8, 4]>>> array[::3][1, 3, 4]>>> array[::4][1, 6] 如果想让他们颠倒形成reverse函数的效果>>> array[::-1][4, 8, 6, 3, 5, 2, 1]>>> array[::-2][4, 6, 5, 1]

Feel yourself understand, then a bubble it:

array =[1, 2, 5, 3, 6, 8, 4]fori inrange(len(array) -1, 0, -1):    printi    forj inrange(0, i):        printj        ifarray[j] > array[j +1]:            array[j], array[j + 1] =array[j +1], array[j]printarray

One line of view:

Line 1:array = [1, 2, 5, 3, 6, 8, 4] A disorderly list There's nothing to explain.

Line 2:for I in range (len (array)-1, 0,-1): This is the second of the example given above, we replace it with a range (6,1,-1), meaning 6 to 1 intervals-1, which is the range of flashbacks (2,7,1), These values are then looped to I, then the value of I will be [6, 5, 4, 3, 2]

Line 3:for J in range (0, I): This is a loop-assigned value to J,J will be [0, 1, 2, 3, 4, 5][0, 1, 2, 3, 4][0, 1, 2, 3][0, 1, 2][0, 1]
Then the top two loops nested together will be

I------------6
J------------0j------------1j------------2j------------3j------------4j------------5

I------------5
J------------0j------------1j------------2j------------3j------------4
I------------4
J------------0j------------1j------------2j------------3
I------------3
J------------0j------------1j------------2
I------------2
J------------0j------------1

Line 4:if Array[j] > array[j + 1]:

>>> array = [1, 2, 5, 3, 6, 8, 4]
>>> Array[0]
1
>>> Array[1]
2
>>> Array[2]
5
>>> Array[3]
3
>>> Array[4]
6
>>> Array[5]
8
>>> Array[6]
4
In fact, this is the use of this order of the array = [1, 2, 5, 3, 6, 8, 4] Sort

Line 5:array[j], array[j + 1] = array[j + 1], Array[j] Replace assignment

Line 6: Print out

In fact, to save things, the sort () function can be done in one sentence ....

--eof--

The

Range () function is detailed in Python

Related Article

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.