Parse the python dictionary constructor dict (mapping)

Source: Internet
Author: User

Python dictionary constructor has three types: dict (), dict (** args), dict (mapping ), the first and second constructors are easier to understand and use,

However, the use of dict (mapping) constructor is hard to understand.

1 dict () constructor can return an empty dictionary

In [7]: d = dict()In [8]: print d{}In [9]: 

2 dict (** arg) constructor. You can use a value assignment expression to input parameters. Multiple Value assignment expressions can be used, separated by commas.

In [9]: d = dict(a = 12, b = 13, c = 15)In [10]: print d{'a': 12, 'c': 15, 'b': 13}In [11]: 

3 dict (mapping): How to input this mapping parameter to construct a python dictionary constructor? What is mapping in Python?

See the example below.

def fmap(a, b):         return (a, b)lik = range(1, 9) liv = list("abcdefgh")print map(fmap, lik, liv) 

The running result is as follows:

[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (7, 'g'), (8, 'h')]

The role of the map function is to retrieve an element value from the iteratable object (here is the list lik and liv) each time. After being processed by the fmap user-defined function, it is used as a new (returned) list elements. Therefore, the map function operates like list parsing.

After understanding the map function, you can use the returned value as the input parameter of dict to obtain 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 result is as follows:

{1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j'}


In the preceding implementation using the map function, Python provides another function zip and can also construct a mapping object. The Code is as follows:

In [77]: k = list("abcdefghij")In [78]: print k['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']In [79]: v = range(11, 21)In [80]: print v[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]In [81]: m = zip(k,v)In [82]: print m[('a', 11), ('b', 12), ('c', 13), ('d', 14), ('e', 15), ('f', 16), ('g', 17), ('h', 18), ('i', 19), ('j', 20)]In [83]: d = dict(m)In [84]: print d{'a': 11, 'c': 13, 'b': 12, 'e': 15, 'd': 14, 'g': 17, 'f': 16, 'i': 19, 'h': 18, 'j': 20}


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.