A summary of common operations on the python list and a summary of python operations
This article provides a detailed description of the List Operation Method in Python for your reference. The details are as follows:
1. Create a list. You only need to enclose different data items separated by commas (,) in square brackets.
List = ['wade ', 'James', 'bosh', 'haslem']
Like the string index, the List Index starts from 0. List can be intercepted, combined, etc.
2. Add new elements
List. append ('allen ') # Method 1: add the parameter object >>> a = [1, 2, 4] >>> a to the end of the list. append (5) >>> print (a) [1, 2, 3, 4, 5] List. insert (4, 'Lewis ') # Method 2: insert an element parameter 1: index location parameter 2: object >>> a = [1, 2, 4] >>>. insert (2, 3) >>> print (a) [1, 2, 3, 4] List. extend (tableList) # method 3: extended list, parameter: iterable parameter >>>> a = [, 3] >>> B = [, 6] >>>. extend (B) >>> print (a) [1, 2, 3, 4, 5, 6]
3. traverse the list
for i in List: print i,
4. Values in the access list
Use subscript indexes to access values in the list. You can also use square brackets to intercept characters, as shown below:
>>> List = [1, 2, 3, 4, 5, 6, 7 ]>>> print(List[3])4
5. Delete elements from the list
List. remove () # Delete Method 1: If the parameter object contains duplicate elements, only the top one will be deleted >>> a = [1, 2, 3] >>>. remove (2) >>> print (a) [1, 3] List. pop () # Delete Method 2: pop optional parameter index Delete the element at the specified position is the last element by default >>> a = [1, 2, 3, 4, 5, 6] >>>. pop () 6 >>> print (a) [1, 2, 3, 4, 5] del List # Delete method 3: You can delete the entire List or specify elements or List slices, the list cannot be accessed after it is deleted. >>> A = [1, 2, 3, 4, 5, 6] >>> del a [5] >>> print (a) [1, 2, 3, 4, 5] >>> del a >>> print (a) Traceback (most recent call last): File "<pyshell #93>", line 1, in <module> print ()
6. sort and reverse code
List. reverse () >>> a = [1, 2, 3, 4, 5, 6] >>>. reverse () >>> print (a) [6, 5, 4, 3, 2, 1] List. sort () # sort has three default parameters cmp = None, key = None, reverse = False. Therefore, you can set the sorting parameters >>> a = [2, 4, 6, 7, 3, 1, 5] >>>. sort () >>> print (a) [1, 2, 3, 4, 5, 6, 7] # In python3X, numbers and characters cannot be sorted together, the following error occurs: >>> a = [2, 4, 6, 7, 3, 1, 5, 'a'] >>>. sort () Traceback (most recent call last): File "<pyshell #104>", line 1, in <module>. sort () TypeError: unorderable types: str () <int ()
7. truncate the Python list
The Python list truncation is of the same type as the string operation, as shown below:
L = ['spam', 'spam', 'spam! ']
Operation:
Python expression result description L [2] 'spam! 'Read the third element in the list L [-2] 'spam' read the second and last elements in the list L [1:] ['spam', 'spam! '] Truncates the list from the second element.
8. Functions and methods for Python list operations
The list operations include the following:Function:
1. cmp (list1, list2): Compares the elements in two lists (python3 has been discarded)
2. len (list): number of list elements
3. max (list): returns the maximum value of the list element.
4. min (list): returns the minimum value of the list element.
5. list (seq): Convert tuples to lists.
List Operations common operations include:Method:
1. list. append (obj): Add a new object to the end of the list.
2. list. count (obj): counts the number of times an element appears in the list.
3. list. extend (seq): append multiple values in another sequence at the end of the list (extend the original list with the new list)
4. list. index (obj): locate the index location of the first matching item of a value from the list.
5. list. insert (index, obj): insert objects to the list.
6. list. pop (obj = list [-1]): removes an element from the list (the last element by default) and returns the value of this element.
7. list. remove (obj): removes the first matching item of a value in the list.
8. list. reverse (): Elements in the reverse list
9. list. sort ([func]): sorts the original list.
The above is all the content of this article. I hope it will help you learn python programming.