The Python dictionary has three constructors, Dict (), Dict (**args), dict (mapping), and the first and second constructors are better understood and easier to use.
The use of the dict (mapping) constructor is more difficult to understand.
1 Dict () constructor can return an empty dictionary
2 Dict (**arg) constructor, the passed-in parameter is an assignment expression, which can be multiple assignment expressions. You can do this with a comma interval.
3 Dict (mapping) constructs the Python dictionary constructor. How do you pass this mapping argument? What exactly is the mapping under Python?
Look at the example below.
Def Fmap (A, B):
Execution results such as the following
[(1, ' a '), (2, ' B '), (3, ' C '), (4, ' d '), (5, ' E '), (6, ' F '), (7, ' G '), (8, ' h ')]
The role of the map function is to take an element value each time from an iterative object (this is the list lik and Liv). After Fmap's own definition of the function 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 get a dictionary by passing the return value as an incoming argument to the Dict.
Def Fmap (A, B): return (A, b) lik = range (1, one) Liv = List ("Abcdefghij") Lim = Map (Fmap, Lik, liv) d = Dict (lim) Print D
The results of the operation are as follows:
{1: ' A ', 2: ' B ', 3: ' C ', 4: ' d ', 5: ' E ', 6: ' F ', 7: ' G ', 8: ' H ', 9: ' I ', ' J '}
The above is implemented by the map function, Python provides a function zip. You can also construct a mapping object, as shown in the following code:
In [all]: K = List ("Abcdefghij") in [+]: Print k[' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ']in [+]: v = range (11, 21 in [+]: Print v[11, (+), +, +, +, +, +, +, 20]in [Bayi]: M = Zip (k,v) in []: Print m[(' A ', one-to-one), (' B ', ' One '), (' C ', ), (' d ', +), (' E ', ' K '), (' F ', +), (' G ', '), (' H ', +), (' I ', +), (' J ', h)]in ["]: D = dict (m) in [+]: Print d{' a ' : One, ' C ': +, ' B ': +, ' e ': +, ' d ': +, ' g ': +, ' F ': +, ' I ': +, ' h ': +, ' J ': 20}
Python dictionary constructor Dict (mapping) parsing