1 #list:2name = ["LC","PXM","PT"]3 Print('------------', Name[2],"----------")4 5 #list slices6 Print(Name[1:3])#slices7 Print(Name[:3])8 Print(Name[-1])#take the last one .9 Print(name[-2:])#take the last two digitsTen One #List Additions AName.append ("Huanglei")#finally add an object -Name.insert (1,"xulingling")#inserts an object at the specified location - the #List Delete -Name.remove ("Lvcheng")#Delete directly, delete the specified object - delName[1] -Name.pop (1)#Delete the last object by default, specify a location to delete + - #List Modification +name[1]="smq" A Print(name) at - #List Operations -Name.index ("smq")#find where objects correspond -Name.count ("Lvcheng")#number of occurrences of an object in the statistics list -Name.reverse ()#List Reversal -Name.sort ()#list sort, by ASCII inName2 = [A] -Name.extend (name2)#list extension, extending another list to the list toName.clear ()#Clear List + delName2#Delete List - the #list replication: *name = ["LC","PXM",["Alex","Jack"],"PT"] $name2 = Name.copy ()#List copy, copy as shallow copy, does not copy list within listPanax Notoginseng Print(name) - Print(name2) thename[2][0]="ALEXANDER" + Print(name) A Print(name2)
1 #List Deep Copy2 ImportCopy3name = ["LC","PXM",["Alex","Jack"],"PT"]4Name2 =copy.deepcopy (name)5 Print(name)6 Print(name2)7name[2][0]="ALEXANDER"8 Print(name)9 Print(name2)
code Execution Results ['LC','PXM', ['Alex','Jack'],'PT']['LC','PXM', ['Alex','Jack'],'PT']['LC','PXM', ['ALEXANDER','Jack'],'PT']['LC','PXM', ['Alex','Jack'],'PT']
1name = ["LC","PXM",["Alex","Jack"],"PT","GGG","Dex","Fed","FCK"]2 #Step slices3 Print(Name[0:-1:2])#indicates how much from the beginning of the printed list to how many ends, and the last represents the interval4 Print(Name[::2])5 #List Loops6 forIinchName:7 Print(i)
Python----data type----list