List summary in Python

Source: Internet
Author: User

Go from Python to list summary

A list can be seen as a data structure and a class,

With Help (list) can see its methods, elements of the additions and deletions have a variety of ready-made methods,

Second, the list operation contains the following functions:
1. CMP (List1, List2): Compare elements of two lists
2. Len (list): Number of elements
3. Max (list): Returns the maximum value of the element
4, Min (list): Returns the minimum value of the list element
5. List (seq): Convert tuples to lists


Third, the following methods are included:
1. List.append (obj): Add a new object at the end of the list
2. List.count (obj): Count the number of occurrences of an element in a list
3. List.extend (seq): Append multiple values from another sequence at the end of the list (extend the original list with a new list)
4, List.index (obj): Find the index position of the first occurrence of a value from the list
5. List.insert (index, obj): inserting objects into the list
6, List.pop (Obj=list[-1]): Removes an element from the list (the last element by default), and returns the value of the element
7, List.remove (obj): Removes the first occurrence of a value in a list
8, List.reverse (): Reverse List of elements
9. List.sort ([func]): Sort the original list **python the difference between sort sorted () reverse () reversed () * *

I want to say that append () and extend () obviously have a lot of differences, append directly after the addition, extend equivalent to the expansion.

Four, list and string conversion, the string split () splits it into a list,join () method to synthesize a list of strings;

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

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

V. Filtering of the list

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

Six, the dictionary parsing (using the list of filter)

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

List summary in Python

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.