"Python" 11, Python's Dictionary of built-in data structures

Source: Internet
Author: User


First, the dictionary

1, the initialization of the dictionary

A dictionary is a key-value structure

in [160]: d = {}in [161]: type (d) Out[161]:  dictin [166]: d = {' A ':1,  ' B ': 2}in [167]: dout[167]: {' a ':  1,  ' B ':  2}in [180]: d = dict ({"A":0,  "B": 1}) in [181]: dout[181]:  {' A ': 0,  ' B ':  1}in [164]: d = dict ([["A",  1], ["B",  2] ])    # The elements of an iterative object must be a two-tuple in [165]: dout[165]: {' a ': 1,  ' B ':  2}in  [168]: d = dict.fromkeys (Range (5)) #  The iterated element passed in as key, with a value of nonein [169]: dout[169] :  {0: none, 1: none, 2: none, 3: none, 4: none}in [170] :  d = dict.fromkeys (Range (5),  "ABC")   #  incoming iteration element is key, value is abcin [171]:  dOut[171]: {0:  ' abc ', 1:  ' abc ', 2:  ' abc ', 3:  ' abc ',  4:   ' abc '} 


Second, the basic operation of the dictionary

1, increase

In [173]: D = {' A ': 1, ' B ': 2} # directly using key as an index, assigning a value to a non-existent index increases KV to in [174]: d["c"] = 1In [175]: dout[175]: {' A ': 1, ' B ': 2, ' C ': 1}in [175]: dout[175]: {' A ': 1, ' B ': 2, ' C ': 1}in [176]: d["b"] = 1In [177]: dout[177]: {' A ': 1, ' B ': 1, ' C ': 1}## di Ct.update () in [178]: D.update ((("D", 4), ("E", 5)) in [179]: dout[179]: {' A ': 1, ' B ': 1, ' C ': 1, ' d ': 4, ' E ': 5}




"Python" 11, Python's Dictionary of built-in data structures

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.