List common operation instances in python

Source: Internet
Author: User
This article mainly introduces list common operations in python, and provides detailed analysis of list creation, addition, deletion, search, filtering, and other operations in the form of instances, for more information about list operations in python, see the examples in this article. Share it with you 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', 'mpilgrim', 'z'] >>> li[0:3]  ['a', 'b', 'mpilgrim'] 

3. add 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(["two", "elements"]) >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']

4. search for list

>>> 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 "
 
  ", line 1, in ?ValueError: list.index(x): x not in list>>> "c" in liFalse
 

5. delete elements from the list

>>> 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 "
 
  ", 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

>>> 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

>>> 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

>>> 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

>>> 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

>>> 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

>>> 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.

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.