List common operation instances in python
This example describes the list common operations in python. Share it with you for your reference. The specific analysis is as follows:
1. Define list
?
1 2 3 4 5 6 7 |
>>> Li = ["a", "B", "mpilgrim", "z", "example"] >>> Li ['A', 'B', 'mpilgrim', 'z', 'example '] >>> Li [0] 'A' >>> Li [4] 'Example' |
2. Negative list Index
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
>>> 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', 'mpilgrim', 'z'] >>> Li [0: 3] ['A', 'B', 'mpilgrim'] |
3. add elements to the list
?
1 2 3 4 5 6 7 8 9 10 11 |
>>> 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 (["two", "elements"]) >>> Li ['A', 'B', 'new', 'mpilgrim', 'z', 'example ', 'new', 'two', 'elements'] |
4. Search for list
?
1 2 3 4 5 6 7 8 9 10 11 12 |
>>> Li ['A', 'B', 'new', 'mpilgrim', 'z', 'example ', 'new', 'two', 'elements'] >>> Li. index ("example ") 5 >>> Li. index ("new ") 2 >>> Li. index ("c ") Traceback (innermost last ): File "<interactive input>", line 1, in? ValueError: list. index (x): x not in list >>> "C" in li False |
5. Delete elements from the list
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
>>> Li ['A', 'B', 'new', 'mpilgrim', 'z', 'example ', 'new', 'two', 'elements'] >>> Li. remove ("z ") >>> Li ['A', 'B', 'new', 'mpilgrim', 'example ', 'new', 'two', 'elements'] >>> Li. remove ("new ") >>> Li ['A', 'B', 'mpilgrim', 'example ', 'new', 'two', 'elements'] >>> Li. remove ("c ") Traceback (innermost last ): File "<interactive input>", line 1, in? ValueError: list. remove (x): x not in list >>> Li. pop () 'Elements' >>> Li ['A', 'B', 'mpilgrim', 'example ', 'new', 'two'] |
Remove removes the first appearance of a value from the list.
Remove only deletes the first appearance of a value. Here, 'new' appears twice in the list, but li. remove ("new") only deletes the first appearance of 'new.
If no value is found in the list, Python raises an exception to respond to the index method.
Pop deletes the last element of the list and returns the value of the deleted element.
6. list Operator
?
1 2 3 4 5 6 7 8 9 10 |
>>> Li = ['A', 'B', 'mpilgrim'] >>> Li = li + ['example ', 'new'] >>> Li ['A', 'B', 'mpilgrim', 'example ', 'new'] >>> Li + = ['two'] >>> Li ['A', 'B', 'mpilgrim', 'example ', 'new', 'two'] >>> Li = [1, 2] * 3 >>> Li [1, 2, 1, 2, 1, 2] |
7. Use join link list to become a string
?
1 2 3 4 5 |
>>> 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 the list where the element is a string. It does not perform any type forced conversion. An exception is thrown when a list with one or more non-string elements is connected.
8. Split the string
?
1 2 3 4 5 6 7 8 |
>>> 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 the opposite of join. It splits a string into multiple element lists.
Note that the separator (";") is completely removed and does not appear in any element in the returned list.
Split accepts an optional second parameter, which is the number of times to be split.
9. list ing Parsing
?
1 2 3 4 5 6 7 8 |
>>> Li = [1, 9, 8, 4] >>> [Elem * 2 for elem in li] [2, 18, 16, 8] >>> Li [1, 9, 8, 4] >>> Li = [elem * 2 for elem in li] >>> Li [2, 18, 16, 8] |
10. parsing in dictionary
?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>>> 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 in 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', 'pwd = secret'] |
11. list filtering
?
1 2 3 4 5 6 7 |
>>> 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'] >>> [Elem for elem in li if li. count (elem) = 1] ['A', 'mpilgrim', 'foo', 'C'] |
I hope this article will help you with Python programming.