One, list operation
list=['Xiaohei','Xiaobai','Xiaohong']#define an arrayName=[]#define an empty array#Check#Subscript is starting from 0Print(list[1])#-1 represents the last elementPrint(list[-1])#Increase#AppendList.append ('Xiaokeke')#InsertList.insert (0,'Xiaojuan')#Changelist[0]='xiaoming'#count determines that there are several duplicate elementsPrint(List.count ('Xiaohei'))#Index finds the subscript of an element: if there is more than one, returns the first, or an error if the element does not existPrint(List.index ('Xiaohei'))#Delete element: Delete the last element by default, specify the subscript to delete the specified element, without this bidding clubs errorList.pop () List.pop (1) List.remove ('Xiaohei')#Remove can delete only one element#Clear Clear Listlist.clear ()#Reverse Reverse listList.reverse ()#Sortlist2=['1','2','5','3']list2.sort ()#The default is ascendingList2.sort (Revserse=true)#Descending#Multidimensional Arrayslist3=[123,1245,1355,[123,1345,45]]list4=[123,1245,1355,[123,[129,124],45]]Print(list4[4][2][1])#Merge two listsList3.extend (LIST4)
Second, list cycle and slice
names=['Xiaohei','Xiaohong','Xiaobai']#Loops forNameinchnames:Print(name)#Traditional Cycle Modeindex=0classs=['123','124',' the'] forIinchRange (3): Print(Names[index]) index+=1#Slice, which is a way of list valuePrint(Names[0:3])Print(Names[3:5])#The slice is Gu Tou regardless of the trailing value of this elementPrint(Names[:6])#value starting from 0Print(names[3:])#starting from subscript 3 to the endPrint(names[:])#Take all ValuesNums=['1','2','3','4','5','6']Print(Nums[::3])#Step Interval 3 to take 1 eachPrint(Nums[::-1])#step is negative, value from right to left
Three
Python Learning Note 2--dict