From a strange piece of code in the Socket module learning
Preface: In learning the socket module in the Python standard library, a strange piece of code was found.
Import Socket def get_constants (prefix):d icts=dict ((GetAttr (Socket,n), N) forin dir ( SOCKET) if n.startswith ('ipproto_'))Print (dicts)
Question: For in the above code. In.. Both the loop statement and the IF statement do not end with a colon. Why?
Answer: Because the "exception" statement above produces a list of mappings (Value,name).
First I noticed that the for and if statements are in the same parentheses, and that both the for and if statements are not complete blocks of statements, but rather part of an object/parameter.
The statement is complete in the following way:
for inch if n.startswith ('ipproto_'))
Therefore, you need to query how to construct a Dictionary object with Dict ().
Help (Dict)
| Dict ()| from a mapping object's | | if | d =| for inch | D[k] =| Dict (**kwargs), new dictionary initialized with the name=| in the keyword argument list. For example: dict (one=1, two=2)
is not the first case (no parameters) and the fourth case (Name=value pairs).
The second case: what is mapping?
Dict (mapping) constructs the Python dictionary constructor, how to pass this mapping parameter? What exactly is the mapping under Python?
There are currently three methods known to pass in mapping.
One, method one, by using the map function
Def Fmap (A, B):
Return (A, B)
Lik = Range (1, 11)
Liv = List ("Abcdefghij")
Print map (fmap, Lik, liv)
The operation results are as follows
[(1, ' a '), (2, ' B '), (3, ' C '), (4, ' d '), (5, ' E '), (6, ' F '), (7, ' G '), (8, ' H '), (9, ' I '), (Ten, ' J ')]
The purpose of the map function is to take an element value from an iterative object (this is the list lik and Liv) each time, and after the Fmap custom function is processed as the new (returned) list element, the map function works much like the concept of list parsing.
Once you understand the map function, you can use the return value as an incoming parameter to Dict to get a dictionary.
Def Fmap (A, B):
Return (A, B)
Lik = Range (1, 11)
Liv = List ("Abcdefghij")
Lim = Map (Fmap, Lik, liv)
D = Dict (Lim)
Print D
The execution results are as follows:
{1: ' A ', 2: ' B ', 3: ' C ', 4: ' d ', 5: ' E ', 6: ' F ', 7: ' G ', 8: ' H ', 9: ' I ', ' J '}
Two, method two: through the zip function
K=[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' J ']
V=[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
M=zip (K,V)
Dict (m) ={' a ': one, ' C ': +, ' B ': +, ' e ': ' d ': +, ' g ': +, ' F ': +, ' I ': +, ' h ': +, ' J ': 20}
Three, method three: By using the list map
A list map transforms data by applying a function to each element of the list.
What is a list map?
Introduction to List Mappings
Li = [1, 9, 8, 4]for in[2,,, 8]li [1, 9, 8, 4]
Li Li Elem.
elem , and then appends the result to the return list.
Note List mappings do not change the list that is mapped
>>> params = {"Server":"Mpilgrim","Database":"Master","UID":"SA","pwd":"Secret"}>>>Params.keys () ['Server','UID','Database','pwd']>>> [k forKinchParams.keys ()] 1['Server','UID','Database','pwd']>>> [Params[k] forKinchParams.keys ()] 2['Mpilgrim','SA','Master','Secret']>>> ["%s=%s"% (k, params[k]) forKinchParams.keys ()] 3['Server=mpilgrim','Uid=sa','Database=master','Pwd=secret'] >>> ["%s=%s"% (k, params[k]) for K in Params.keys () if K.startswith (' s ')]
[' Server=mpilgrim '] 1An example of a simple list map. The mapping expression is exactly the element itself, so this list map returns the intact copy of the list. It is equal to Params.keys ().2a more difficult mapping. Iterate through Params.keys (), the variable k is assigned to each element sequentially, the map expression receives the element, and the corresponding value is found in the dictionary params. It is equal to Params.values (). 3using some simple string formatting to merge the previous two examples, we get a key .-a list of value pairs. This looks a bit like the output of the program, and all that is left is to link the elements in the list to form a string. |
|
|
|
Therefore, a particular list of constructs can be called dict (list) to convert the list object to a Dict object.
Perhaps, this special structure of the list is mapping.
So the above code:
If N.startswith ('ipproto_'))
The (GetAttr (socket,n), n) for N in dir (socket) if N.startswith ('ipproto_') part is actually the code that produces a list.
That is [(GetAttr (Socket,n), n) for N in dir (socket) if N.startswith (' Ipproto_ ')].
Python--socket module and list mapping