Python: Array/list (remove () function, append () function, sort () function, reverse () function)

Source: Internet
Author: User
Tags for in range

Sort:

1: Finishing Order

 #   bubbling  lista = [5,7,11,19,99,63,3,9,1]list  = []  while  lista!= []: number  = 0  for  i in  Span style= "COLOR: #000000" > lista:  if  number < I:number  = I lista.remove (number) list.append (number)  print  (list)  
  print result: D:\untitled\  1\ Venv\scripts\python.exe d:/untitled/1/venv/main.py[ 99, 63, 19, 11, 9, 7, 5, 3, 1< Span style= "COLOR: #000000" >]process finished with exit code 0   
 #   Select  lista = [5,7,11,19,99,63,3,9,1]list  = []  while  lista!= []:  for  i in   Lista:number = 1 for  J in   lista:  if  number < J:  Number  = J Lista.remove (number) list.append (number)  print  (list)  
Printed results: D:\untitled\1\venv\scripts\python.exe d:/untitled/1/venv/main.py[99, 63, 19, 11, 9, 7, 5, 3, 1 ]process finished with exit code 0

2:sort () function

# sort function lista =[5,7,11,19,99,63,3,9,1]lista.sort ()print(lista)
Printing results:
D:\untitled\1\venv\Scripts\python.exe d:/untitled/1/venv/main.py[1, 3, 5, 7, 9, one, a,,]process Finished with exit code 0

3: Add a number

#if a=18, add this number sequentially in the original listList = [1,4,7,9,11,14,19,34,60,79,98]Print("Previous:%s"%list) a= 18List.append (a) list_1= [] whileList! =[]: number= 100 forIinchlist:ifI <Number:number=I list.remove (number) list_1.append (number)Print("After:%s"%list_1)
Printed results: D:\untitled\1\venv\scripts\python.exe d:/untitled/1/venv/main.py ago: [1, 4, 7, 9, 11, 14, 19, 34, 98]: [1, 4, 7, 9, one, 0,,,, 98]process finished with exit code

Instance:

3*3 table

 #   3*3 table  listx = [1,2,3 for  i in   listx:  for  j in   listx:  if  i >= j:x  = I*j  print  ( "  %s*%s=%s   "% (i,j,x), End="   " )  print  () 
Print Result: D:\untitled\1\venv\scripts\python.exe d:/untitled/1/venv/main.py1*1=1 2*1=2 2*2=4 3*1=3 3*2= 6 3*3=9 Process finished with exit code 0

List Reverse Print: List_num = ["1", "2", "3"] Print order: 3,2,1

Use the Len () function

 #   Reverse list (len () method returns the length of an object (character, list, tuple, and so on) or the number of items  list_num = [ " 1   ", "  2   ", "  3   " ]i  = Len (list_num)  print  (List_num[i-1::-1])  
Printing results:
D:\untitled\1\venv\Scripts\python.exe d:/untitled/1/venv/main.py['3" 2'1']process finished with exit code 0

Use the reverse () function

 list_num = [ " 1   ", "  2   ", "  3   " ]list_num.reverse ()  print  (list_num)  
Print Result: D:\untitled\1\venv\scripts\python.exe d:/untitled/1/venv/main.py['3 ' ' 2 ' ' 1 ' ]process finished with exit code 0

Using the For statement

A = [1,2,3,4,5,6= len (a) for in range (int (NUM/2)):    -i-1] = a[num-i-1 ],a[i]print(a)
D:\untitled\1\venv\Scripts\python.exe d:/untitled/1/venv/main.py[6, 5, 4, 3, 2, 1]process finished With exit code 0

Small knowledge:

List_num = ["1","2","3"]   for in list_num[::-1]:# forward value    print(i)
Print Result: D:\untitled\1\venv\scripts\python.exe d:/untitled/1/venv/main.py321Process Finished with exit code 0

Python: Array/list (remove () function, append () function, sort () function, reverse () function)

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.