Python Map List Instance analysis

Source: Internet
Author: User
The examples in this article describe the Python mapping list. Share to everyone for your reference. The specific analysis is as follows:

List mapping is a very useful way to transform data by applying a function to each element of the list, and you can use a strategy or method to traverse the calculation of each element.

For example:
Copy the Code code as follows:

params = {"Server": "Mpilgrim", \
"Database": "Master", \
"UID": "sa", \
"PWD": "Secret" \
}
Print ["%s=%s"% (k, params[k]) for K in Params.keys ()]
print '; '. Join (["%s=%s"% (k, params[k]) for K in Params.keys ()])

The returned result is:
Copy the Code code as follows:

[' Uid=sa ', ' pwd=secret ', ' database=master ', ' Server=mpilgrim ']



It can also be improved by converting tuples into strings, using the Join function method.
Copy CodeThe code is as follows:

' Uid=sa;pwd=secret;database=master;server=mpilgrim '


(the Join function is not only used for tuples but also for lists, functions are strings)
Split with join instead, convert string to Narimoto Group
See the following applications
Copy CodeThe code is as follows:

str = ' Server=mpilgrim;uid=sa;database=master;pwd=secret '>>> Str.split (';')
[' Server=mpilgrim ', ' uid=sa ', ' database=master ', ' Pwd=secret ']
>>> str.split ('; ', 1)
[' Server=mpilgrim ', ' Uid=sa;database=master;pwd=secret ']
>>> str.split ('; ', 2)
[' Server=mpilgrim ', ' uid=sa ', ' Database=master;pwd=secret ']

List map Use cases:
Copy the Code code as follows:

>>>> Li = [1, 9, 8, 4]
>>> [elem*2 for Elem in Li] 1
[2, 18, 16, 8]
>>> Li
[1, 9, 8, 4]

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