Several ways to implement Python list inversion

Source: Internet
Author: User
Tags python list


Here are a few different implementations of the functions

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)

The Ps:resv2 () method changes the order of the original list, while others do not

Some basic usage of list

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 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 "<interactive input>", line 1, in?
ValueError:list.index (x): X not in List
>>> "C" in Li
False

5. Remove an element 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.re Move ("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 ()      & nbsp; 
    ' elements '
    >>> li
    [' A ', ' B ', ' Mpilgrim ', ' example ', ' new ', ' two ']

Remove removes the first occurrence of a value from the list.
Remove deletes only the first occurrence of a value. Here, ' new ' appears in the list two times, but Li.remove ("new") only deletes 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 ', ' 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 '

A join can only be used for a list of elements that are strings; It does not cast any type. Connecting a list that has one or more non-string elements throws an exception.


8. Split 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, which splits a string into a multiple-element list.
Notice 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 argument, which is the number of times to split.

Mapping resolution for 9.list

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

Parsing in the 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 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 Filter

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

Related Article

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.