ArticleDirectory
- Create
- Value in the access dictionary
- Update dictionary
- Delete dictionary elements and dictionaries
- Dictionary built-in method
Create
Method 1:
>>> Dict1 ={}>> dict2 = {'name': 'global', 'Port': 80 }>>> dict1, dict2 ({}, {'Port': 80, 'name': 'earth '})
Method 2: from Python 2.2, you can use a factory method to input a list of tuples as parameters.
>>> Fdict = dict (['x', 1], ['y', 2]) >>> fdict {'y': 2, 'x ': 1}
Method 3:
From Python 2.3, you can use fromkeys (), a convenient built-in method, to create a "default" dictionary. The elements in the dictionary have the same value (if not given, the default value is none, which is a bit like the oneobject method of my framework ):
>>> Ddict = {}. fromkeys ('x', 'y'),-1) >>> ddict {'y':-1, 'x ': -1 }>>>>>> edict = {}. fromkeys ('foo', 'bar') >>> edict {'foo': None, 'bar': None}
Value in the access dictionary
To traverse a dictionary (usually using a key), you only need to view its key cyclically, as shown in the following code:
>>> Dict2 = {'name': 'Earth ', 'Port': 80 }>>>>>> for key in dict2.keys ():... print 'key = % s, value = % s' % (Key, dict2 [Key])... key = Name, value = earthkey = port, value = 80
From Python 2.2, You can traverse the dictionary directly in the for loop.
>>> Dict2 = {'name': 'Earth ', 'Port': 80 }>>>>>> for key in dict2 :... print 'key = % s, value = % s' % (Key, dict2 [Key])... key = Name, value = earthkey = port, value = 80
To determine whether a key-Value Pair exists, you can use the has_key () or in or not in operators.
>>> 'Server' in dict2 # Or dict2.has _ key ('server') False >>> 'name' in dict # Or dict2.has _ key ('name ') true >>> dict2 ['name'] 'global'
An example of mixing numbers and strings in a dictionary:
>>> Dict3 ={}>> dict3 [1] = 'abc' >>> dict3 ['1'] = 3.14159 >>> dict3 [3.2] = 'xyz' >>> dict3 {3.2: 'xyz', 1: 'abc', '1': 3.14159}
Update dictionary
Update by overwriting
In the preceding example, dict2 ['name'] = 'global ';
Update dict2 ['name'] = 'abc ';
Delete dictionary elements and dictionaries
Del dict2 ['name'] # delete an entry whose key is "name"
Dict2.clear () # delete all entries in dict2
Del dict2 # Delete the entire dict2 dictionary
Dict2.pop ('name') # Delete and return an entry whose key is "name"
Dict2 = {'name': 'earth', 'Port': 80 }>>> dict2.keys () ['Port ', 'name'] >>>>> dict2.values () [80, 'global'] >>>>> dict2.items () [('Port', 80 ), ('name', 'global')] >>>>> for eachkey in dict2.keys ():... print 'dict2 key', eachkey, 'has value', dict2 [eachkey]... dict2 key port has value 80dict2 key name has value Earth
The Update () method can be used to add the content of a dictionary to another dictionary.
Dict3 = {'server': 'http', 'Port': 80, 'host': 'venus' }>>> dict3.clear () >>> dict3 {}
Ing type related functions
>>> Dict (x = 1, y = 2) {'y': 2, 'x': 1 }>>> dict8 = dict (x = 1, y = 2) >>>> dict8 {'y': 2, 'x': 1 }>>> dict9 = dict (** dict8) >>> dict9 {'y': 2, 'x': 1} dict9 = dict8.copy ()
Dictionary built-in method
Method Name |
Operation |
Dict. Clear () |
Delete all elements in the dictionary |
Dict. Copy () |
Returns a copy of the dictionary. |
Dict. fromkeysc (SEQ, val = none) |
Create and return a new dictionary. Use the elements in seq as the dictionary key, and Val as the initial values corresponding to all keys in the dictionary (if this value is not provided, the default value is none) |
Dict. Get (Key, default = none) |
Returns the value corresponding to the Key key in the dictionary dict. If the key does not exist in the dictionary, the default value is returned (note that the default value of the parameter is none) |
Dict. has_key (key) |
If the key exists in the dictionary, true is returned; otherwise, false is returned. After the in and not in versions of python2.2 are introduced, this method is almost obsolete, but one Workable interfaces. |
Dict. Items () |
Returns a list of tuples containing Dictionary (key, value) pairs. |
Dict. Keys () |
Returns a list of dictionary types. |
Dict. Values () |
Returns a list containing all values in the dictionary. |
Dict. ITER () |
Methods iteritems (), iterkeys (), and itervalues () are the same as their non-iterative methods. The difference is that they return an iterator instead of a list. |
Dict. Pop (Key [, default]) |
Similar to the get () method, if the key in the dictionary exists, delete it and return the dict [Key]. If the key does not exist and no default value is given, a keyerror exception is thrown. |
Dict. setdefault (Key, default = none) |
Similar to the method set (), if the dictionary does not contain a key, the value is assigned by dict [Key] = default. |
Dict. setdefault (Key, default = none) |
Similar to the method set (), if the dictionary does not contain a key, the value is assigned by dict [Key] = default. |