Python dictionary constructor Dict (mapping) parsing

Source: Internet
Author: User

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 parameters with an assignment expression, can be multiple assignment expressions, with a comma interval.


3 Dict (mapping) constructs the Python dictionary constructor, how to 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), and after the fmap of its own defined 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, also can construct a mapping object, code such as the following see:

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

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.