Python uses a For loop to sort elements in a list

Source: Internet
Author: User
This article describes how Python uses a for loop to sort elements in a list

List = [6, 11]for m in range (len (list)-1): As    N in range (m+1, Len (list)):        if list[m]> list[n]:
  
   temp = List[n]            list[n] = list[m]            list[m] = tempprint List
  

Results:

[6, 11, 13, 22, 99]


Analysis:

List = [6, 11]for I in range (Len (list)-1): For    J in range (I+1,len (list)): #        Print str (i) #        print "I nit--"+ str (j)        print ' comparison list subscript value ' + str (i), str (j)        if list[i] > list[j]:#            print i,j#            print '-----  ' + str (list[i]) + str (list[j]) + '-------'            T = list[j]            list[j]= list[i]            list[i]=t#            print T #            Print LIST[I],LIST[J]            print ' ******** internal loop *************** '        Print List     print  list    print ' ++++ ++++ This internal cycle ends +++++++\n\n '

--from the printed information can be seen in the cycle process--

Results:

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.