Data structure of Python: List

Source: Internet
Author: User

List: Data structure for handling a set of Ordered items 1, list operator List1=[2,3,4,5,6,7,8]print len (list1) print [1,2]+[3,4]print [' Hi ']*4print 4 in List1for I in List1:print ilist2=list1list3=list1[:]print list2 are list1 trueprint list3 is list1 falseshoplist = [' Apple ', ' MONGO ', ' Carrot ', ' banana '] 2, new element shoplist.append (' rice ') print U ' new shopping list ', shoplist result: New shopping list [' Apple ', ' MONGO ', ' carrot ', ' Banana ', ' rice '] 3, delete an element List1=[1,2,3,4,5,6,7]list1.remove (3) value operation, no return value del list1[2] subscript operation List1.pop (2) subscript operation, with return value  4, compares elements in two lists for equality CMP (LIST1,LIST2) 1,0,-1 return value List1 = = List2str (list1) = = str (list2)  5, remove the largest element in the list: two methods Max ( List1) List1.sort () list1[-1] 6, insert an element List1.insert (2,1024) Insert 1024 element  7, merge list lista=[1,2,3]listb=[in place labeled 2 9,4,5]lista.extend (LISTB) or ListA = Lista+listbprint ListA Result: [1, 2, 3, 9, 4, 5] 8, two-dimensional list listc=[[1,2,3],[21,22,23]] Print U ' first element ', Listc[0]print u ' first element ', listc[0][0] Result: first element [1, 2, 3] first element 1 9, statistic list [1,2,3,4,5,6,3,4,5,6,3] 3 occurrences , not less than two methods List1.count (3) sum=0for i in list1:if i = = 3:sum +=1print sum 10. Determine if a variable belongs to a class if Isinstance (listc,list):p rint ' OK ' result: OK or: if Type (LISTC) = = list: 11, reverse list list1=list (reversed (List1)) List1.reverse () directly modifies the List1 and does not need to assign a value to the List1 list1=list1[::-1]  get the subscript of the element List1.index (454) for I in Xrange (Len (list1)): If list1[i] = = 454:print Ibreak Second, advanced application 1, slice Description: Lista[startponit:endponit:step (step)], after slicing to get a new list Startpoint:endpoint Front closed   (1) Slicing operation, very important lista=[1,2,3,4,5,6] print Lista[0:4] results: [1, 2, 3, 4] print Lista[0:4:2] Results: [1, 3]   (2) step is negative, which enables reverse output print lista[::-1] Results: [6, 5, 4, 3, 2, 1] 2, List changed to String listb=[' a ', ' B ', ' C ']print '. Join (LISTB) Results: abc 3, List Builder (1) The first range (A:B:C) list1=range () Print list1 results: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] list2=range (2,10) p Rint list2 Results: [2, 3, 4, 5, 6, 7, 8, 9] list3=range (2,10,2) Print list3 results: [2, 4, 6, 8]  (2) The second type of list4=[x*x for x in RA Nge (]print) list4 results: [0, 1, 4, 9, A, A-Z,, $, a, 81] LIST3 = [m+n for M in ' ABC ' for n ' XYZ '] results: AX,AY,AZ,BX, By,bz,cx,cy,cz list1 = [1,2,3,4]LIST2 = [2*i for i in LIst1 if i>2] list3 = [m+n for M in ' ABC ' for n ' XYZ '] dict4 ={' x ': ' A ', ' y ': ' B ', ' Z ': ' C '}list4 = [k + ' = ' +v for K,v in Dict4.items ()] l = [' Hello ', ' World ', ' IBM ', ' Apple ']list5 = [S.lower () for S in L] 4, sort () method sort (cmp=n ONE,KEY=NONE,REVERSE=FALSE) CMP: Default by ASCII value comparison, can be defined using other values for comparison CMP: For a custom comparison function, accept two parameters, and if the first argument is less than the second argument, returns a negative number, is greater than returns an integer, and so on returns 0. The default value is None. Key: An element in the default list. From what to compare, you can customize the function Key's usage scene to compare multiple reverse: Default ascending, Reverse=true, reverse eg:list1=[(1,5,3), (1,1,2,3,4), (1,6)]def L (tup): Return Len ( TUP) List1.sort (key=l,reverse=true) Print list1 eg:list1 = [( -1,5,3), ( -5,3,6,3), (1,1,2,4,5,6), (2,9), ( -2,10)], Use the tuple's first element size comparison to sort the list, in reverse order. List1 = [( -1,5,3), ( -5,3,6,3), (1,1,2,4,5,6), (2,9), ( -2,10)]def L (TUP): Return abs (Tup[0]) List1.sort (cmp=cmp,key=l, reverse=true) Print list1  

Data structure of Python: list

Related Article

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.