Python Learning Series (4) (List and functions)

Source: Internet
Author: User
Tags function examples

Python Learning Series (4) (List and functions)
I. Basic Concept 1. What is a list? A sequence data type. An ordered data set is enclosed by square brackets when used with commas. 2, several access Forms 1) index access, syntax structure: list_name [index]. Note that the index value of the list starts from 0. For example: 1 >>> li = [1, 3, 4, 5] 2 >>> print li [2], li [0] 3 3 1 Delete the list items: 1 >>> del li [2] 2 >>> print li3 [1, 2, 4, 5] 2) slice access, syntax structure: list_name [start_index, end_index, step]. Example: 1 >>> li = [1, 3, 4, 5] 2 >>> print li [2: 4] 3 [3, 4] 4 >>> li = [1, 2, 3, 4, 5] 5 >>> print li [] 6 [3] Delete list items: 1 >>> del li [] 2 >>> print li3 [1, 2, 5] 3, basic operation 1) addition: combines multiple lists into a new and longer list. 1 >>> li = [1, 3, 4, 5] 2 >>> li1 = range (6, 9) 3 >>>> li2 = li + li14 >>> print li25 [1, 2, 3, 4, 5, 6, 7, 8] 2) multiplication: equivalent to adding n times to the same list. 1 >>> print li1 * 22 [6, 7, 8, 6, 7, 8] 3) (not) in: contains or not, determines whether a value is a list element. 1 >>> print 2 in li2 True3 >>> print 10 in li24 False5 >>> print 10 not in li26 True4) traversal: each element in the access list uses the for loop to retrieve the values of each element in the list one by one. Copy code 1 >>> li1 = [1, 3, 4, 5] 2 >>> li = li1 * 3 >>>> I = 0 4 >>> for val in li: 5 print 'Li [% d] '% (I), val 6 I + = 1 7 (space, Enter key run) 8 li [0] 1 9 li [1] 210 li [2] 311 li [3] 412 li [4] 513 li [5] 114 li [6] 215 li [7] 316 li [8] 417 li [9] 518 li [10] 119 li [11] 220 li [12] 321 li [13] 422 li [14] 5 copy the code list parsing: [val_expr for val in list_name], where: val_expr: Is the calculation expression of the variable val. val is used to store the value of each element retrieved from the list_name list, use the value of each val_expr as an element for creating a new list. Exercise: use the list to calculate 1 ~ 9. 1 >>> li = range (1, 10) 2 >>> print li3 [1, 2, 3, 4, 5, 6, 7, 8, 9] 4 >>> li1 = [x ** 3 for x in li] # list resolution 5 >>> print li16 [1, 8, 27, 64,125,216,343,512,729] 2. There are many related functions in the related function list, which can be viewed through the help function. Now let's take several important function examples for your understanding. 1) len () function: it is a Python built-in function that does not belong to any data type. Generally, it can be used to measure the length of sequence data. 1 >>> li = range (1, 11) 2 >>> print li3 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 4 >>> print len (li) 5 102) count () function: counts the number of times an element appears in the list. Copy code 1 >>>> li = range (1, 3) 2 >>> li2 = li * 33 >>> print li24 [1, 2, 1, 2, 1, 2] 5 >>> print li2.count (li2 [2]) 6 37 >>> print li2.count ('A') 8 0 copy code 3) insert function: add the object to the specified position in the list. The elements in the list are removed sequentially. Copy code 1 >>>> li = [] 2 >>> li = range () 3 >>> print li 4 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 5 >>> li. insert (5, 10) 6 >>> print li 7 [1, 2, 3, 4, 5, 10, 6, 7, 8, 9, 10] 8 9 >>> li. insert (6, li1) 10 >>> li11 [1, 2, 3, 4, [5, 6, 7, 8, 9], [5, 6, 7, 8, 9] Copy code 4) append function: adds an object to the end of the list. Example 1: insert an object as a single element in the list. Copy the code >>> li = [] >>> li1 = range () >>> for x in li1: li. append (x) >>> print li [1, 2, 3, 4] Copy code Example 2: The inserted object is a list. Copy code 1 >>> li = [] 2 >>> li1 = range () 3 >>> for x in li1: 4 li. append (x) 5 6 7 >>> print li 8 [1, 2, 3, 4] 9 >>> li1 = range (5, 10) 10 >>> li. append (li1) 11 >>> print li12 [1, 2, 3, 4, [5, 6, 7, 8, 9] Copy Code 5) extend function: add all elements in a list to the end of the list as an individual. Copy code 1 >>> li1 = range () 2 >>> li2 = range () 3 >>> print li1, '\ n', li24 [1, 2, 3, 4, 5] 5 [6, 7, 8, 9] 6> li1.extend (li2) 7> li18 [1, 2, 3, 4, 5, 6, 7, 8, 9] Copy Code 6) remove function: Delete the specified element that appears for the first time in the list. Copy code 1 >>>> li = range () * 32 >>> print 'lil', li3 li [1, 2, 3, 1, 2, 3, 1, 2, 3] 4> print 'Li = ', li5 li = [1, 2, 3, 1, 2, 3, 1, 2, 3] 6> li. remove (2) 7 >>> print 'Li = ', li8 li = [1, 3, 1, 2, 3, 1, 2, 3] Copy code 7) pop function: deletes the element at the specified position in the list or the tail element of the list. Copy code 1 >>> li = range () * 3 2 >>> print 'Li = ', li 3 li = [1, 3, 1, 2, 3, 1, 2, 3] 4 >>> li. pop () 5 3 6 >>> li 7 [1, 3, 1, 2, 3, 1, 2] 8 >>> li. pop (3) 9 210 >>> li11 [1, 3, 1, 3, 1, 2] copy the code exercise: 1. Use a function to remove repeated elements in the table I) remove function implementation: Copy code 1 >>> li = 'www .baidu.com '* 10 2 >>> li = list (li) 3 >>> li. sort () 4 >>> I = 0 5 >>> for s in li: 6 while li. count (s)> 1: 7 li. remove (s) 8 I + = 1 9 10 11 >>> print li 12 ['. ', 'A',' B ',' C ', 'D',' I ', 'M', 'O', 'U', 'w'] Copy code ii) pop function implementation: copy the Code 1 li = [, 3, 3, 4] 2 length = len (li) 3 print li 4 pos = length-1 5 while pos> = 0: 6 r = li. count (li [pos]) 7 if r> 1: 8 I = 0 9 while I <R-1: 10 li. pop (pos) 11 pos = pos-112 I = I + 113 else: 14 print 'over! '15 pos-= 116 print li copy code iii) extend function and append function implementation (if you are interested, try again) 2. Remove the two-dimensional list [] and change the two-dimensional to one-dimensional. I) nested loop implementation: The isinstance function can determine the data type. Copy the Code 1 li = [, 4, range (), 5, range (), 6] 2 print li 3 k = 0 4 for li1 in li: 5 if isinstance (li1, list): 6 j = 0 7 for li2 in li1: 8 li. insert (k + j, li2) 9 li. remove10 j = j + 111 del li [k + j] 12 k = k + 113 print li copy code ii) Single Loop implementation: Copy code 1 li = [, 4, range (1, 4), 5, range (1, 4), 6] 2 print li3 B = [] 4 for I in li: 5 if isinstance (I, list): 6 B. extend (I) 7 else: 8 B. append (I) 9 print (B) Copy code 3. Structure and access of Tuple tuples. The usage is basically the same as that of the list. There are two differences: I) Enclose each data item using parentheses (). The element values of the I) tuples cannot be modified. Example: Copy code 1 >>> t = tuple (range () 2 >>> print t3 (1, 2, 3, 4, 5, 6, 7, 8, 9) 4 >>> print t [3] 5 46 >>> print t [2: 8] 7 (3, 4, 5, 6, 7, 8)

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.