Python-Python tutorial

Source: Internet
Author: User
This article mainly introduces how to implement list inversion in Python. The example summarizes various common list operation skills, for more information about how to implement list inversion in Python, see the examples in this article. Share it with you for your reference. The specific implementation method is as follows:

There are several different functions below

The code is as follows:

Import math

Def resv (li ):
New = []
If li:
Cnt = len (li)
For I in range (cnt ):
New. append (li [cnt-i-1])
Return new

Def resv2 (li ):
Li. reverse ()
Return li

Def resv3 (li ):
Hcnt = int (math. floor (len (li)/2 ))
Tmp = 0
For I in range (hcnt ):
Tmp = li [I]
Li [I] = li [-(I + 1)]
Li [-(I + 1)] = tmp
Return li

Li = [1, 2, 3, 4, 5]

Print resv (li)


Ps: The resv2 () method will change the sorting of the original list, but the rest will not
Basic list usage
1. Define list

The code is as follows:

>>> Li = ["a", "B", "mpilgrim", "z", "example"]
>>> Li
['A', 'B', 'mpilgrim', 'z', 'example ']
>>> Li [0]
'A'
>>> Li [4]
'Example'



2. negative list index

The code is as follows:

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

The code is as follows:

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

The code is as follows:

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



5. delete elements from the list

The code is as follows:

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

The code is as follows:

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

The code is as follows:

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

The code is as follows:

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

The code is as follows:

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

The code is as follows:

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

The code is as follows:

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