The example in this article describes the list common operations in Python. Share to everyone for your reference. The specific analysis is as follows:
1. Define List
>>> li = ["A", "B", "Mpilgrim", "Z", "example"]>>> li [' A ', ' B ', ' Mpilgrim ', ' z ', ' example ']>>> Li[0] ' a ' >>> li[4] ' example '
2. Negative list Index
>>> Li [' A ', ' B ', ' Mpilgrim ', ' z ', ' example ']>>> li[-1] ' example ' >>> li[-3] ' Mpilgrim ' >> ;> Li [' A ', ' B ', ' Mpilgrim ', ' z ', ' example ']>>> li[1:3] [' B ', ' Mpilgrim '] >>> li[1:-1] [' B ', ' MPI Lgrim ', ' z '] >>> Li[0:3]
3. Adding elements to the list
>>> Li [' A ', ' B ', ' Mpilgrim ', ' z ', ' Example ']>>> li.append ("new") >>> Li [' A ', ' B ', ' Mpilgrim ', ' Z ', ' example ', ' New ']>>> Li.insert (2, "new") >>> Li [' A ', ' B ', ' new ', ' Mpilgrim ', ' z ', ' example ', ' new ' ]>>> Li.extend (["Both", "elements"]) >>> Li [' A ', ' B ', ' new ', ' Mpilgrim ', ' z ', ' example ', ' new ', ' both ', ' Elements ']
4. Search List
>>> Li [' A ', ' B ', ' new ', ' Mpilgrim ', ' z ', ' example ', ' new ', ' both ', ' Elements ']>>> li.index ("Example") 5 & Gt;>> Li.index ("new") 2 >>> Li.index ("C") Traceback (innermost last): File "
", line 1, in? ValueError:list.index (x): X not in List>>> "C" in Lifalse
5. Deleting an element from the list
>>> Li [' A ', ' B ', ' new ', ' Mpilgrim ', ' z ', ' example ', ' new ', ' both ', ' Elements ']>>> li.remove ("z") &G T;>> Li [' A ', ' B ', ' new ', ' Mpilgrim ', ' example ', ' new ', ' both ', ' Elements ']>>> li.remove ("new") >>> ; Li [' A ', ' B ', ' Mpilgrim ', ' example ', ' new ', ' both ', ' Elements ']>>> li.remove ("C") Traceback (innermost last) : File "
", line 1, in? ValueError:list.remove (x): X not in List >>> li.pop () ' elements ' >>> li [' A ', ' B ', ' Mpilgrim ', ' Exa Mple ', ' new ', ' both ']
Remove removes the first occurrence of a value from the list.
Remove simply removes the first occurrence of a value. Here, ' new ' appears two times in the list, but Li.remove ("new") only removes the first occurrence of ' new '.
If no value is found in the list, Python throws an exception in response to the index method.
Pop does two things: delete the last element of the list, and then return the value of the deleted element.
6.list operator
>>> li = [' A ', ' B ', ' Mpilgrim ']>>> li = li + [' Example ', ' new ']>>> li [' A ', ' B ', ' Mpilgrim ', ' E Xample ', ' new ']>>> li + = [' both ']
7. Use the Join link list to become a string
>>> params = {"Server": "Mpilgrim", "Database": "Master", "UID": "sa", "pwd": "Secret"}>>> ["%s=%s"% (K, V) for K, V in Params.items () [' Server=mpilgrim ', ' uid=sa ', ' database=master ', ' Pwd=secret ']>>> ";". Join (["%s=%s"% (k, v) for K, V in Params.items ()]) ' Server=mpilgrim;uid=sa;database=master;pwd=secret '
Join can only be used for lists where the element is a string; It does not make any type casts. Connecting a list that exists with one or more non-string elements throws an exception.
8. Splitting a string
>>> li = [' Server=mpilgrim ', ' uid=sa ', ' database=master ', ' pwd=secret ']>>> s = ";". Join (LI) >>> s ' Server=mpilgrim;uid=sa;database=master;pwd=secret ' >>> s.split (";") [' Server=mpilgrim ', ' uid=sa ', ' database=master ', ' Pwd=secret ']>>> s.split (";", 1) [' Server=mpilgrim ', ' uid= Sa;database=master;pwd=secret ']
Split is just the opposite of join, which splits a string into a multi-element list.
Note that the delimiter (";") is completely removed, and it does not appear in any of the elements in the returned list.
Split accepts an optional second parameter, which is the number of times to split.
Mapping resolution for 9.list
>>> Li = [1, 9, 8, 4] >>> [elem*2 for Elem in Li]
Parsing in 10.dictionary
>>> params = {"Server": "Mpilgrim", "Database": "Master", "UID": "sa", "pwd": "Secret"}>>> Params.keys ( [' Server ', ' uid ', ' database ', ' pwd ']>>> params.values () [' Mpilgrim ', ' sa ', ' master ', ' secret ']>>> Params.items (' server ', ' Mpilgrim '), (' uid ', ' sa '), (' Database ', ' Master '), (' pwd ', ' secret ')]>>> [k for K, V I n Params.items () [' Server ', ' uid ', ' database ', ' pwd ']>>> [V for K, V in Params.items ()] [' Mpilgrim ', ' sa ', ' Master ', ' Secret ']>>> ["%s=%s"% (k, v) for K, V in Params.items ()] [' Server=mpilgrim ', ' uid=sa ', ' database=master ', ' PW D=secret ']
11. List filtering
>>> li = ["A", "Mpilgrim", "foo", "B", "C", "B", "D", "D"]>>> [Elem for Elem in Li if Len (elem) > 1][ ' Mpilgrim ', ' foo ']>>> [elem for Elem in Li if elem! = ' B ' [' A ', ' Mpilgrim ', ' foo ', ' C ', ' d ', ' d ']>>> [el Em for Elem in Li if Li.count (elem) = = 1][' A ', ' Mpilgrim ', ' foo ', ' C ']
Hopefully this article will help you with Python programming.