List method,

Source: Internet
Author: User

List method,
# The method is a function closely related to objects. objects may be lists, numbers, strings, or other types of objects. The preceding example shows the call method of a method. The call syntax is as follows: object. method (parameter) # It can be seen from the preceding syntax and the example of the append () method: The method is defined to put the object before the method name, and the two are separated by a dot before, parameters can be included in the brackets after the method. Besides syntax differences, method calls are similar to function calls. # The list contains commonly used methods such as count, index, and sort. The following describes them one by one. 1. append

1 # This method has been introduced before. The function is to add a new object at the end of the list. 2 # usage: 3 >>> list. append (obj)
2. count

1 # count () is used to count the number of times an element appears in the list. The syntax of the 2 # count () method is as follows: 3 >>> list. count (obj) 4 # In this syntax, list represents the list, and obj represents the objects in the list. 5 # Example 6 >>> field = list ('hello, World') 7 >>> field 8 ['h', 'E', 'l ', 'l', 'O', ',', 'w', 'O', 'R', 'l ', 'D'] 9 >>> print ('number of letters o in the field List: ', field. count ('O') 10 in the field list, the number of letters o: 211 >>>> linxuyue = list ('xiaotao') 12 >>> print ('list linxuyue, the number of letters o is: ', linxuyue. count ('O') 13 in the linxuyue list, the number of letters o is: 214 15 >>> listobj = [26, 'Hello', 'World ', 26] 16 >>> listobj17 [26, 'Hello', 'World', 26] 18 >>> print ('Number 26 :', Listobj. count ('26') 19 Number 26: 020 >>> print ('Number 26: ', listobj. count (26) 21 Number 26: 222 # note that single quotation marks are not required for numeric type. 23 >>> print ('Hello count: ', listobj. count ('hello') 24 hello: 125 >>> listobj. count (26) 26 227 >>> min = [228], [], >>> min29 ([1, 3], 5, 6, [1, 3], 2) 30 >>> print ('number of [1, 3] In the nested list min is: ', min. count ([1, 3]) 31) the number of [1, 3] In the nested list min is: 232 >>>> print ('number of 5 in the nested list min is: ', min. count (5) 33 the number of 5 in the nested list min is: 134 >>> print ('number of 1 in the nested list min is: ', min. count (1) 35 the number of 1 in the nested list min is: 036 >>> min. count ([1]) 37 0
3. extend

1 # extend () method is used to append multiple values in another sequence at a time at the end of the list (use a new list to expand the original list .) 2 syntax of the extend () method: 3 >>> list. extend (seq) 4 # list represents the list in this syntax, and seq represents the list of elements. 5 >>> a = ['hello', 'World'] 6 >>> B = ['python', 'is, 'funny '] 7 >>>. extend (B) 8 >>> a 9 ['hello', 'World', 'python', 'is ', 'funny '] 10 # the above operation results look like a connection operation. What is the difference between the extend () method and the sequence addition? 11 >>> a = ['hello', 'World'] 12 >>> B = ['python', 'is ', 'funny '] 13 >>> a + b14 ['hello', 'World', 'python', 'is ', 'funny '] 15 >>> a16 ['hello', 'World'] 17 # From the output result, we can see that the values of a and B in the two examples are the same, however, the value of output a in the first example is different from that of Output a in the second example. 18 # The main difference between the extend () method and the sequence addition is that the extended sequence is modified by the extend () method, as shown in the previous; the original connection operation returns a new list. In the example above, a new list containing copies a and B is returned without modifying the original variables. 19 # Of course, you can also use the partition assignment previously learned to achieve the same result, as shown below: 20 >>> a = ['hello ', 'World'] 21 >>> B = ['python', 'is ', 'funny '] 22 >>> len (a :) 23 >>> a [len (a):] = b24 >>> a25 ['hello', 'World ', 'python', 'is, 'funny '] 26 # You can see that the output result is the same as that of the extend () method, but it does not seem easy to understand, therefore, this solution is not selected.
4. index
1 # index () is used to locate the index location of the first matching item of a value from the list. 2 # syntax of the index () method: 3 >>> list. index (obj) 4 # list represents the list in this syntax, and obj represents the object to be searched. 5 # example: 6 >>> Yuan Wei = ['000000', 'hangzhou', 'hangzhou', 'job', 'job ', 'Room '] 7 >>> print ('index location:', Yuan Wei. index ('job') 8. The index location is: 3 9 >>> Yuan Wei. index (2017) 10 Traceback (most recent call last): 11 File "<pyshell #39>", line 1, in <module> 12 Yuan Wei. index (2017) 13 ValueError: 2017 is not in list14 >>> Yuan Wei. index ('20140901') 15 016 >>> Yuan Wei. index ('hangzhou') 17 218 >>> 19 as shown in the preceding example, search for the text 'job' and find it at the position where the index number is 3; if the index number is 2017, the system will prompt that 2017 is not in the list. The index location of '123' will be correctly indexed only when quotation marks are added.
5. insert

1 # The insert () method is used to locate the index location of the first matching item of a value from the list. 2 # syntax of the insert () method: 3 >>> list. insert (index, obj) 4 # In this syntax, list () represents the list, index represents the index location to be inserted in object obj, and obj represents the object to be inserted in the list. 5 # example: 6 >>> numbers = [1, 2, 3] 7 >>> print ('num: 'before inserting, numbers) 8 insert num before: [1, 2, 3] 9 >>> numbers. insert (2, 'insert after 2, before 3 ') 10 >>> print ('num:', numbers after inserting) 11 num: [1, 2, 'insert after 2, before 3 ', 3] 12 >>> numbers13 [1, 2, 'insert after 2, before 3 ', 3] 14 # As shown in the preceding example, the insert method is very convenient. 15 # Like the extend () method, the operation of the insert () method can also be implemented using the partition assignment we learned earlier. 16 >>> numbers = [, 3] 17 >>> print ('numbers before insertion: ', numbers) 18 numbers before insertion: [1, 2, 3] 19 >>> print ('inserted numbers: ', numbers) 20 inserted numbers: [1, 2,' after 2, 3 before '] 21 # The output result is the same as the insert operation result, but it seems that it is easy to understand that insert is not used.
6. pop

1 # The pop () method is used to remove an element from the list (the last element by default) and return the value of this element. 2 # syntax of the pop () method: 3 >>> list. pop (obj = list [-1]) 4 # In this syntax, "list" indicates the list. "obj" indicates an optional parameter, indicating the object to remove the list element. 5 # example: 6 >>> Yuan Wei = ['2', '0', '1', '7', 'hangzhou', 'hangzhou ', 'job', 'job', 'room'] 7 >>> Yuan Wei. pop () # Without parameters, the last element 8' room '9 >>> print ('yuan Wei: 'After the element is removed, Yuan Wei) is removed by default) 10 Yuan Wei after the element is removed: ['2', '0', '1', '7', 'hangzhou', 'hangzhou', 'work ', 'zuo'] 11 >>> Yuan Wei. pop (2) # Remove element 12 '1' 13> print ('remove Yuan Wei: ', Yuan Wei with element number 2) 14. Remove Yuan Wei with element number 2: ['2', '0', '7', 'hangzhou', 'hangzhou ', 'zuo'] 15 >>> Yuan Wei. pop (0) 16 '2' 17 >>> print ('remove Yuan Wei after the element number is 0: ', Yuan Wei) 18. Remove Yuan Wei after the element number is 0: ['0', '7', 'hangzhou', 'hangzhou ', 'zuo'] 19 # as shown in the example, No parameter is included when the 'room 'is removed. The last one is removed by default. The removal of '1' and '2' is based on the input number parameter. 20 # The pop method is the only list method that can modify the list and return element values (except None. 21 # Use pop to implement a common data structure-stack. 22 # The principle of stack is like stacking a plate. to operate a plate at a time, you need to heap several plates into a pile. You can only put another plate on one plate. To take a plate, it can only be taken one by one from the top, and the last plate is the first to be taken. The stack is also like this. The Last put into the stack is First removed, called LIFO (Last In First Out), that is, the Last First In First Out. 23 # The put and remove operations in the stack have the same name-push and pop ). Pyhton does not have the inbound method, but can be replaced by the append method. The operation results of the pop method and append method are exactly the opposite. If the value of the inbound stack (or append) is just output, the final result will not change, as shown in the following example: 24 >>> numbers = [1, 2, 3] 25 >>> numbers. append (numbers. pop () # append the default outbound stack value 26 >>> print ('numbers append the default outbound stack value. The operation result is: ', numbers) 27 numbers: [1, 2, 3] 28 # As shown in the preceding operation result, by append the default output stack, the worthwhile list is the same as the original list.

7. remove

 

 

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.