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