Pythondictionary (Dictionary)
A dictionary is another mutable container model and can store any type of object.
Each key value of the dictionary (Key=>value) is split with a colon (:), each pair is separated by a comma (,), and the entire dictionary is enclosed in curly braces ({}) , as shown in the following format:
D = {key1  :  value1,  key2  :  value2  }< /c13>
The key must be unique, but the value does not have to be.
The value can take any data type, but the key must be immutable, such as a string, a number, or a tuple.
A simple Dictionary instance:
Dict = {'Alice':  '2341',  ' Beth ' :  ' 9102 ' ,  ' Cecil ' :  ' 3258 ' }
You can also create a dictionary like this:
Dict1 = {  'abc':  456  }; Dict2 = {  'abc':  123,  98.6:  PNs  };
   Accessing values in the dictionary
Put the corresponding key into the familiar square brackets, the following example:
In [161]: a = {"name": "Zpy", "Age": "All"}in [162]: aout[162]: {' age ': ' + ', ' name ': ' Zpy '}in [163]: a[' name ']out[163]: ' Zpy '
Modify Dictionary
The way to add new content to a dictionary is to add a new key/value pair, modify or delete an existing key/value pair as follows:
In [165]: a["age"]= 11In [166]: aout[166]: {' age ': one, ' name ': ' Zpy '}
Add Key-vauel Dictionary
In [167]: a["sex"]= "woman" in [168]: aout[168]: {' age ': one, ' name ': ' zpy ', ' sex ': ' Woman '}
Delete a dictionary element
The ability to delete a single element also clears the dictionary, emptying only one operation.
Show Delete a dictionary with the Del command, as in the following example:
In [a[]: del ' name '] #删除名字key-vaulein [171]: aout[171]: {' age ': one, ' sex ': ' Woman '}in [172]: A.clear () #清空字典In [173 ]: aout[173]: {}in [174]: del a # #删除字典In [175]: A---------------------------------------------------------------------------nameerror Trac  Eback (most recent) <ipython-input-175-60b725f10c9c> in <module> ()----> 1 Anameerror:name ' a ' is Not defined
Properties of Dictionary Keys
A dictionary value can take any Python object without restriction, either as a standard object or as a user-defined one, but not a key.
Two important points to keep in mind:
1) The same key is not allowed to appear two times. When created, if the same key is assigned a value of two times, the latter value is remembered, as in the following example:
In [176]: Dict = {' name ': ' Zara ', ' age ': 7, ' name ': ' Manni '};in [177]: Print dict[' name ']manni
2) The key must be immutable, so it can be used as a number, a string, or a tuple, so you can't use a list.
Dictionary built-in functions & methods
The Python dictionary contains the following built-in functions:
 
 
  
   
   | Serial Number | Functions and descriptions | 
 
   
   | 1 | CMP (Dict1, DICT2) Compares two dictionary elements.
 | 
 
   
   | 2 | Len (dict) Calculates the number of dictionary elements, that is, the total number of keys.
 | 
 
   
   | 3 | STR (DICT) The output dictionary is a printable string representation.
 | 
 
   
   | 4 | Type (variable) Returns the type of the variable entered and returns the dictionary type if the variable is a dictionary.
 | 
 
  
The Python dictionary contains the following built-in methods:
 
 
  
   
   | Serial Number | Functions and descriptions | 
 
   
   | 1 | Dict.clear () Delete all elements in a dictionary
 | 
 
   
   | 2 | Dict.copy () Returns a shallow copy of a dictionary
 | 
 
   
   | 3 | Dict.fromkeys (seq[, Val])) Create a new dictionary with the keys to the dictionary in sequence seq, Val is the initial value corresponding to all keys in the dictionary
 | 
 
   
   | 4 | Dict.get (Key, Default=none) Returns the value of the specified key if the value does not return the default value in the dictionary
 | 
 
   
   | 5 | Dict.has_key (Key) Returns False if the key returns true in the dictionary Dict
 | 
 
   
   | 6 | Dict.items () Returns an array of traversed (key, value) tuples as a list
 | 
 
   
   | 7 | Dict.keys () Returns a dictionary of all keys in a list
 | 
 
   
   | 8 | Dict.setdefault (Key, Default=none) Similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to default
 | 
 
   
   | 9 | Dict.update (DICT2) Update the key/value pairs of the dictionary dict2 to the Dict
 | 
 
   
   | 10 | Dict.values () Returns all values in the dictionary as a list
 | 
 
   
   | 11 | Pop (Key[,default]) Deletes the value of the dictionary given key key, and returns the value to be deleted. The key value must be given. Otherwise, the default value is returned.
 | 
 
   
   | 12 | Popitem () Randomly returns and deletes a pair of keys and values in the dictionary.
 | 
 
  
This article is from the "12462896" blog, please be sure to keep this source http://12472896.blog.51cto.com/12462896/1963830
Python Dictionary Introduction