Common Methods for sharing the Python operation list

Source: Internet
Author: User

The following is a list of common methods and operations and a small example:

1. Append
To add an element at the end of the list, you must add the element at the end of the list. Note the following points:
A. The parameters added in append are as A whole.
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. append (list ("tiger "))
>>> Name
['S ', 'C', 'O', 't', 't', ['', 't',' I ', 'G ', 'E', 'R']

The obtained values are not: ['s ', 'C', 'O', 't', 't', '','t ', 'G', 'E', 'R']
If you want to append this method, you can try the multipart assignment (or the extend method mentioned below ):
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name [len (name):] = list ("tiger") # append from the end
>>> Name
['S ', 'C', 'O', 't', 't', '', 't',' I ', 'G', 'E ', 'R']

B. append can only add one element at a time.
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. append ("A", "B") # An error is returned when multiple elements are added.
Traceback (most recent call last ):
File "<stdin>", line 1, in?
TypeError: append () takes exactly one argument (2 given)
>>> Name. append ("")
>>> Name
['S ', 'C', 'O', 't', 't', 'a']

2. Count

Count the number of times an element appears in the list
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. count ('s ')
1
>>> Name. count ("t ")
2
>>> Name. count ("")
0
>>> Name. append (list ("Python "))
>>> Name
['S ', 'C', 'O', 't', 't', ['P', 'y', 't', 'h ', 'o', 'n']
>>> Name. count (['P', 'y', 't', 'h', 'O', 'n'])
1

3. Extend

Append multiple values of another sequence to the original list

Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. extend (list ("tiger "))
>>> Name
['S ', 'C', 'O', 't', 't', '', 't',' I ', 'G', 'E ', 'R']

Of course, we can use the multipart value assignment to implement:
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name [len (name):] = list ("tiger ")
>>> Name
['S ', 'C', 'O', 't', 't', '', 't',' I ', 'G', 'E ', 'R']

At this time, the friends will think that we can directly use the operator "+", but it is more convenient:
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Pwd = list ("tiger ")
>>> Name + pwd
['S ', 'C', 'O', 't', 't', '', 't',' I ', 'G', 'E ', 'R']
>>> Name
['S ', 'C', 'O', 't', 't']

From the output of these three operations, we can see that:
Both extend and multipart assignment modify the original list. In contrast, extend is more readable, while the operator "+" generates a new list without affecting the original list.
You can use the operator "+" to generate a new list without affecting the original list ".

4. Index
Locate the index location of the first (note that it is the first) matching item of a value from the list.
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. index ('T') # The index location of the first letter t is 3.
3
>>> Name. index ('A ')
Traceback (most recent call last ):
File "<stdin>", line 1, in?
ValueError: list. index (x): x not in list
>>> 'A' in name
False
>>> 'A' not in name
True

From the output, we can see that index is the index location of the first matching item. If the searched element is not in the list, an error is returned (will it be better to return-1 ?), If you want to avoid reporting
An error occurs. We can first use the in operation to determine whether an element is in a list. If so, perform the index operation.

5. Insert
Used to insert an object to the list. The first parameter is the index position and the second parameter is the inserted element object.
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. insert (2, 'tiger ') # insert the string tiger where the index is 2
>>> Name
['S ', 'C', 'tiger', 'O', 't', 't']

We can also assign values by using multipart assignment:
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name [2] = ['tiger ']
>>> Name
['S ', 'C', 'tiger', 'O', 't', 't']
>>> Name [2] = 'tiger'
>>> Name
['S ', 'C', 't',' I ', 'G', 'E', 'R', 'tiger', 'O ', 'T', 't']

Note that if you insert an element, you must use [] to enclose it. Otherwise, if you directly use a string, you need to insert the string list and add it after the index position.
Of course, the readable score of insert is given a high value.

6. Pop
Removes an element from the list (the last element) and returns the value of this element.
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. pop ()
'T'
>>> Name
['S ', 'C', 'O', 't']
>>> Name. append ("t ")
>>> Name
['S ', 'C', 'O', 't', 't']

Partition assignment simulates pop:
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name [len (name)-1:] = []
>>> Name
['S ', 'C', 'O', 't']

The above uses pop and append to simulate the stack's first-in-first-out LIFO.

7. Remove
Remove the first match of a value in the list: if there are two equal elements, only one matching element is removed. If an element does not exist in a list, an error is returned, and only
Removes an element.
Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. remove ("t") # remove the first t
>>> Name
['S ', 'C', 'O', 't']
>>> Name. remove ("A") # An error is reported if no one exists.
Traceback (most recent call last ):
File "<stdin>", line 1, in?
ValueError: list. remove (x): x not in list
>>> "A" not in name
True
>>> Name. remove ("s", "c") # only one element can be removed at a time.
Traceback (most recent call last ):
File "<stdin>", line 1, in?
TypeError: remove () takes exactly one argument (2 given)

8. Revense

Reverse the elements in the list

Copy codeThe Code is as follows:
>>> Name = list ("scott ")
>>> Name
['S ', 'C', 'O', 't', 't']
>>> Name. reverse ()
>>> Name
['T', 't', 'O', 'C', 's']

9. Sort & Sorted

The sort method is used to sort the list. modifying the original list does not return a copy of the sorted list.

Copy codeThe Code is as follows:
>>> Result = [8, 5, 5, 3, 9]
>>> Result. sort ()
>>> Result
[3, 5, 5, 8, 9]

If we want to return a copy of the sorted list without affecting the original list, we can assign values to the original list first (you can assign values to copy the list using multipart assignment), and then
Perform the sort operation on the copied list. Another method is to use the sorted function, which returns the sorted list copy:
Copy codeThe Code is as follows:
>>> Result = [8, 5, 5, 3, 9]
>>> Result2 = sorted (result)
>>> Result
[8, 5, 5, 3, 9]
>>> Result2
[3, 5, 5, 8, 9]

For custom comparison methods, you can input compare functions for sort using javascript, and Comparable <T> instances for java. Python is also similar ~ (@ ^ _ ^ @)~.

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.