List
Table name = ["Name1", "name2"]
Print (Names[1:3]) #切片
The starting position includes, and the end position is not included. will take 1 and 2, without taking 3
names = ["name1","name2","Name3","Name4"]Print(names)Print(names[1],names[2])Print(Names[1:3])#slicesPrint(Names[-1])#take the last one .Print(names[-2:])#take the last twoPrint(Names[0:3])#take the top twoNames.append ("Name5")#insert inside the arrayNames.insert (1,"Name6")#inserts into the array position number 1thNames.insert (3,"Name7")#inserts into the array position number 3rdnames[2]="Name8"#2nd element changed to Name8Names.remove ("Name7")#Delete element Name7delNAMES[2]#remove element Number 2ndNames.pop ()#The default is to delete the last one, enter the subscript to delete the number of elementsPrint(Names.index ("name1"))#where to find name1?Print(Names[names.index ("name1")])#find the name1 and print it out.Names.append ("Name5") Names.append ("Name5")Print(Names.count ("Name5"))#count how many name5Names.reverse ()#List ReversalNames.sort ()#Sort by first alphabetical orderNames2= [1,2,3,4]names.extend (names2)#put another list and come overnames.clear ()#List EmptyPrint(names)
Shallow copy
names = ["name1","name2","Name3",["Tom","Jack"],"Name4","Name5"]name2= Names.copy ()#Copy only the first layer of the list, the second layer is the pointer, when printed from the pointer to find the original addressPrint(names)Print(name2) names[0]="Name 1"names[3][0]="Alex"#name2 also changed inside.Print(names)Print(name2)
Deep copy
ImportCopynames= ["name1","name2","Name3",["Tom","Jack"],"Name4","Name5"]name2= Copy.deepcopy (names)#deep Copy, full copy, second level list does not follow changePrint(names)Print(name2) names[0]="Name 1"names[3][0]="Alex"Print(names)Print(name2)
names = ["name1","name2","Name3",["Tom","Jack"],"Name4","Name5"]Print(Names[0:-1:2])#Start,stop,step
Python 04-List,