Python learning notes (8) Python list (2), python learning notes

Source: Internet
Author: User
Tags python list

Python learning notes (8) Python list (2), python learning notes

List Functions

Append and expand

List. append () append a new object to the end of the list.

1 >>> dir (list) # list functions 2 ['_ add _', '_ class _', '_ contains __', '_ delattr _', '_ delitem _', '_ delslice _', '_ doc _', '_ eq __', '_ format _', '_ ge _', '_ getattribute _', '_ getitem _', '_ getslice __', '_ gt _', '_ hash _', '_ iadd _', '_ imul _', '_ init __', '_ iter _', '_ le _', '_ len _', '_ lt _', '_ mul __', '_ ne _', '_ new _', '_ reduce _', '_ performance_ex _', '_ repr __', '_ reversed _', '_ rmul _', '_ setattr _', '_ setitem _', '_ setslice __', '_ sizeof _', '_ str _', '_ subclasshook _', 'append', 'Count', 'extend', 'index ', 'insert', 'pop', 'delete', 'failse', 'sort '] 3 >>> help (list. append) # help to view list. details of the append () function 4 Help on method_descriptor: 5 6 append (...) 7 L. append (object) -- append object to end # append the object to the end 8 9 >>> a = [] 10 >>>. append (50) # append object 50 to list a 11 >>>> a12 [2, 6, 50] 13 >>>. append ("python book") 14 >>> a15 [2, 6, 50, 'python Book'] 16 >>> a17 [2, 6, 50, 'python Book', ['baidu', 'weibo '] 18 >>> B = [1] 19 >>> id (B) # id return value: the memory space is 20 60126664L21 >>> B. append (5) 22 >>> b23 [1, 5] 24 >>> b25 [1, 5] 26 >>>> id (B) # after appending 5, id returns the same value 27 60126664L28 >>> B. append ("zhangsan") 29 >>> id (B) 30 60126664L31 >>> b32 [1, 5, 'hangsan'] 33 >>>
Note: When a list is modified, the original list is modified instead of a new list.

Extend () append multiple values in another sequence at one time at the end of the list (extend the original list with the new list)

1 >>> help (list. extend) # help to view list. extend function details 2 Help on method_descriptor: 3 4 extend (...) 5 L. extend (iterable) -- extend list by appending elements from the iterable # append the elements in the iteratable object to the list 6 7 >>> a = [1, 2, 3] # iteratable object 8 >>> B = [, 6] # iteratable object 9 >>>. extend (B) 10 >>> a # elements in list B are appended to list a one by one. 11 [1, 2, 3, 4, 5, 6] 12>. extend ("python") 13 >>> a14 [1, 2, 3, 4, 5, 6, 'P', 'y', 't', 'h ', 'o', 'n'] 15 >>> alst = [1, 2] 20 >>> hasattr (alst, '_ iter _') # judge whether iteration is possible, true is returned. Otherwise, false21 True22 >>> hasattr ("python", '_ iter _') # The string cannot be iterated, here, the character of a string is removed and appended with 23 False24 >>>> a = [1, 2] 25 >>>. append ([4, 5]) 26 >>> a27 [1, 2, [4, 5] 28 >>>. extend ([4, 5]) 29 >>> a30 [1, 2, [4, 5], 4, 5] 31 >>> B = [9, 8] 32 >>>. append (B [0]) 33 >>>. append (B [1]) 34 >>> a35 [1, 2, [4, 5], 4, 5, 9, 8] 36 >>>
Note: The difference between append and extend is that extend splits the elements one by one and append the entire append.

Other functions

Count () counts the number of times an element appears in the list.

1 >>> help (list. count) 2 Help on method_descriptor: 3 4 count (...) 5 L. count (value)-> integer -- return number of occurrences of value # return number of occurrences 6 7 >>> a = [, 3, 3] 8>. count (1) #1 This element appears three times in the List 9 310 >>>. count (2) #2 This element appears 3 times in the list 11 312 >>>. count ("a") # a does not have 13 0 in the list

Index () identifies the index location of the first matching item of a value from the list.

1 >>> help (list. index) 2 Help on method_descriptor: 3 4 index (...) 5 L. index (value, [start, [stop])-> integer -- return first index of value. # the value in the parameter is located at the index position 6 for the first time in the list. Raises ValueError if the value is not present. 7 8 >>>. index (1) 9 010 >>>. index (2) 11 312 >>>

Insert () inserts an object into the list.

1 >>> help (list. insert) 2 Help on method_descriptor: 3 4 insert (...) 5 L. insert (index, object) -- insert object before index # insert the object to the first 6 7> a = ["python ", "web"] 8 >>>. insert (1, "aaa") # insert a string aa 9> a10 ['python', 'aaa ', 'web'] 11 >>>. insert (0, "like") # insert a string like12> a13 ['like', 'python', 'aaa' at the beginning ', 'web'] 14 >>>
Note: The insert statement is modified as in-situ and no list is created.

Pop () removes an element from the list (the last element by default) and returns the value of this element.

1 >>> help (list. pop) 2 Help on method_descriptor: 3 4 pop (...) 5 L. pop ([index])-> item -- remove and return item at index (default last ). # Delete the element corresponding to the index and return it as the return value (the last element is deleted by default) 6 Raises IndexError if list is empty or index is out of range. # You cannot delete an element that is empty or out of the index range. Otherwise, index errors 7 8 >>> a 9 ['like', 'python', 'aaa ', 'web'] 10 >>>. pop (1) 11 'python' 12 >>> a13 ['like', 'aaa', 'web'] 14 >>>

Remove () removes the first match of a value in the list.

1 >>> help (list. remove) 2 Help on method_descriptor: 3 4 remove (...) 5 L. remove (value) -- remove first occurrence of value. # Remove a value worthy of the first match. 6. Raises ValueError if the value is not present. 7 8 >>> a = ["test", "test", "demo"] 9 >>>. remove ("test") 10 >>> a11 ['test', 'Demo'] 12 >>>. remove ("aa") 13 Traceback (most recent call last): 14 File "<stdin>", line 1, in <module> 15 ValueError: list. remove (x): x not in list16 >>>

Elements in the reverse list of reverse ()

 1 >>> help(list.reverse) 2 Help on method_descriptor: 3  4 reverse(...) 5     L.reverse() -- reverse *IN PLACE* 6  7 >>> a =[1,2,3,4,5] 8 >>> a.reverse() 9 >>> a10 [5, 4, 3, 2, 1]11 >>>

Sort () to sort the original list

1 >>> help (list. sort) 2 Help on method_descriptor: 3 4 sort (...) 5 L. sort (cmp = None, key = None, reverse = False) -- stable sort * in place *; 6 cmp (x, y)->-1, 0, 1 7 8> a = [5, 9, 3, 1] 9>. sort () #10> a11 [1, 3, 5, 9] 12> B = [9, 3, 8, 6] 13> B. sort (reverse = True) #14> b15 [9, 8, 6, 3] 16 >>>

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.