The example in this article tells a list of Python mappings. Share to everyone for your reference. The specific analysis is as follows:
List mapping is a useful way to transform data by applying a function to each element of the list, and you can use a policy or method to traverse the calculation of each element.
For example:
Copy 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 result returned is:
Copy Code code as follows:
[' Uid=sa ', ' pwd=secret ', ' database=master ', ' Server=mpilgrim ']
It can also be improved by converting tuples into strings and using the Join function method.
Copy Code code as follows:
' Uid=sa;pwd=secret;database=master;server=mpilgrim '
(the Join function is not only used for tuples but also for lists, and functions are strings)
Split with join instead, convert string to infinitesimal group
See the following applications
Copy Code code as follows:
str = ' Server=mpilgrim;uid=sa;database=master;pwd=secret ' </span></pre>>>> 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 mapping Use cases:
Copy 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]
I hope this article will help you with your Python programming.