Python data type (list) learning notes, python learning notes
List
Creation list:
Name_list = ['Alex ', 'seven', 'Eric'] Or name_list = list (['Alex ', 'seven', 'Eric'])
In fact, the main content of today's learning is
Index
Slice
Append
Delete
Length
Slice
Loop
Include
Index and slice:
You can use subscript for flexible values.
Define a list name = ["baobao", "wangdleong", "wangwenzhe", "wangweiyi", "wangda", "wanger"]
Name = ["baobao", "wangdleong", "wangwenzhe", "wangweiyi", "wangda", "wanger"] print (name [0]) # print (name [2]) by default starting from 0 # obtain the third value print (name [0: 2]) # retrieve the value print (name [-1]) between zero and two # retrieve the last value print (name [: 2]) # print (name [-3:]) using the step size # print (name [: 2] [0]) from the last to the last # retrieve the step size as 2, print (name [: 2] [0] [0]) # Slice the value again. name [0] = "baoshan" # Use the subscript to modify the content name. insert (3, "wangdejiang") # insert the name of the position where the subscript is 3 directly. append ("lingling") # append the content to the final name. remove ("baoshan") # directly Delete the print (name) Content)
The printed content is as follows:
Exercises:
Name = ["baobao", "wangdleong", "xiaodongfang", "wangwenzhe", "wangweiyi", "wangda", "wanger"] name. insert (-1, "dalongfeng") # insert the name before the subscript-1. insert (3, "dabaozi ") # insert to the position where the subscript is 3 name2 = name [] # define a variable that equals the first subscript to the value print (name2) print (name) between 2 and 7) del name [7] # Use del to delete the inserted content through subscript (if it is continuous, use [2: 5]) del name [3] name [1] = "wangdelongzuzhang" # Use modifications to add remarks print (name) print (name [: 2]) # Use step size for isolated printing, the number of isolates depends on the step size.
The printed content is as follows:
List copying and other operations:
Name = ["baobao", "wangdleong", 25,634,478, 9,424, 9, "xiaodongfang", "wangwenzhe", "wangweiyi", "wangda", "wanger, 25,631,474, 99] print (9 in name) # determine whether the list contains Element 9. If there is a true print (name. count (9) # determine the number of 9 elements in the list: print (name. index (9) # Find the first sub-value name2 = ["dede", 2, "sdjagoew"] name that contains the element. extend (name2) # merged list name [0] = "BAOBAO" print (name) name3 = name. copy () # copy name becomes name3print (name3) name. reverse () # Flip list # name. sort () # sort. After 3.0, you can only sort strings by name2.pop () # Delete the last name2.pop (1) by default # use subscript to delete the specified value print (name2) print (name) # You can use the loop to modify all the equivalent elements in the list for I in range (name. count (9): name [name. index (9)] = 999999 print (name)
Printed result:
Comparison of light copy
Name = ["baobao", "wangdleong", [9,44, 25,634,478, 9], "xiaodongfang", "wangwenzhe", "wangweiyi", "wangda", "wanger ", 9,424, 25,631,474,] name3 = name. copy () name [0] = "BAOBAO" # name [2] [1] = 58247 # name3 will be changed if name3 in the Set chat table is changed to name3. [2] [2] = "DDDDDD" # If name3 is used for modification, it will also be changed. # The reason is that the set list in the list uses a separate memory space print (name) print (name3)
Deep copy application, there is a len on how to view the number of elements below
Import sys, copyname = ["baobao", "wangdleong", [25,634,478, 9], "xiaodongfang", "wangwenzhe", "wangweiyi", "wangda ", "wanger", 9,424, 25,631,474,] name3 = name. copy () name4 = copy. copy (name) # similar to the neme copy, it is a shortest copy name5 = copy. deepcopy (name) # Deep copy, is to directly copy a memory block name [0] = "BAOBAO" # the change in this location will not be taken to name3 to name [2] [1] = 58247 # If the set chat table name3 will change to name3 [2] [2] = "DDDDDD" # If name3 is used, it will also be changed. # The reason is that the set list in the list is used. is a separate memory space print (name) print (name3) print (name4) print (name5)
Print (len (name5) # list length, number of elements
Print (id (name), id (name3), id (name4), id (name5) # You can view the memory location by id