Python Learning Note 2--dict

Source: Internet
Author: User

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

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.