Tutorial on dictionary ing type in Python, python dictionary
The dictionary is the only ing type in python. It is represented by braces {}. A dictionary entry is a key-value pair. The method keys () returns the dictionary Key List, values () returns the list of dictionary values. items () returns the list of dictionary key-value pairs. There are no restrictions on the values in the dictionary. They can be any python object, but the keys in the dictionary have type restrictions. Each key can only correspond to one value, and the keys must be ha-compatible, all unchangeable types are hashable. The frozenset element of an unchangeable set can be used as the dictionary key, but the variable set cannot.
The following are common dictionary types.
Clear (): delete all elements in the dictionary.
Copy (): returns a copy of the Dictionary (Shortest copy.
Fromkeys (seq, val = None): Creates and returns a new dictionary, uses the elements in seq as the dictionary key, and val as the initial values corresponding to all keys in the dictionary.
Get (key, default = None): return the value corresponding to the key in the dictionary. If the key does not exist in the dictionary, the default value is returned.
Has_key (key): If the key exists in the dictionary, True is returned; otherwise, False is returned. After python2.2, this method is almost obsolete and usually replaced by in.
Items (): returns a list of key pairs in the dictionary.
Keys (): returns a list containing the dictionary key.
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.
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, the default value is not provided, causing a KeyError.
Setdefault (key, default = None): similar to the get () method. If the key does not exist in the dictionary, the value is assigned by dict [key] = default.
Update (dict2): add the dictionary dict2 key-value pairs to the current dictionary.
Values (): returns a list Of all values in the dictionary.
The key can be of multiple types, but the key is unique and unique. The value can be unique.
>>> D = {'A': 1, 'B': 2 }>>> d {'B': 2, 'A ': 1 }>>> L = [('jonh', 18), ('nancy ', 19)] >>> d = dict (L) # create from the list containing key values >>> d {'jonh': 18, 'nancy ': 19 }>>> T = tuple (L) >>> T ('jonh', 18), ('nancy ', 19)> d = dict (T) # create a key value using a tuple >>> d {'jonh': 18, 'nancy ': 19 }>>> d = dict (x = 1, y = 3) # create with keyword parameters >>> d {'X': 1, 'y': 3 }>>> d [3] = 'Z' >>>> d {3: 'Z', 'x': 1, 'y': 3}
Another method to create a dictionary is fromkeys (S [, v]). In python, the explanation is New dict with key from S and value equal to v, that is, the elements in S are used as keys, v is the value of all keys. The default value of v is None. You can call d. fromkeys (S [, v]) through an existing dictionary d, or call dict. fromkeys (S [, v]) by type.
>>> d {3: 'z', 'y': 3} >>> L1 = [1,2,3] >>> d.fromkeys(L1) {1: None, 2: None, 3: None} >>> {}.fromkeys(L1,'nothing') {1: 'nothing', 2: 'nothing', 3: 'nothing'} >>> dict.fromkeys(L1,'over') {1: 'over', 2: 'over', 3: 'over'}
The dictionary is unordered, so you cannot obtain the value through the index. You must use the key to find the associated value. An error KeyError occurs for keys that do not exist.
>>> d {3: 'z', 'x': 1, 'y': 3} >>> d[3] 'z' >>> d['x'] 1 >>> d[0] Traceback (most recent call last): File "<pyshell#26>", line 1, in <module> d[0] KeyError: 0
Dictionary operations and methods:
Len (d) returns the number of key-value pairs in dictionary d.
X in d: query whether the dictionary d contains a key x.
>>> d = {'x':1,'y':3} >>> len(d) 2 >>> 'x' in d True >>> 'z' not in d True
D [x] = y if the key x exists, change the value corresponding to x to y. If the key x does not exist, add the key-Value Pair x: y to the dictionary d.
>>> d {'x': 1, 'y': 3} >>> d['x'] = 1.5 >>> d {'x': 1.5, 'y': 3} >>> d['z'] = 5 >>> d {'z': 5, 'x': 1.5, 'y': 3}
Del d [x] deletes the key-value pairs with the key-value pairs in the dictionary d. If x does not exist, a KeyError occurs.
>>> d = {'z': 5, 'x': 1.5, 'y': 3} >>> del d['x'] >>> d {'z': 5, 'y': 3} >>> del d['x'] Traceback (most recent call last): File "<pyshell#66>", line 1, in <module> del d['x'] KeyError: 'x'
D. clear () clear the dictionary d
D. copy (): perform a light copy on the dictionary d, and return a new dictionary with the same key-value pair as d.
>>> d {'z': 5, 'y': 3} >>> d.copy() {'z': 5, 'y': 3}
D. get (x [, y]) returns the value of key x in the dictionary d. If key x does not exist, return y. The default value of y is None.
>>> d = {'z': 5, 'x': 1.5, 'y': 3} >>> d.get('x') 1.5 >>> del d['x'] >>> d.get('x') >>> d.get('x','nothing') 'nothing'
D. items () returns all key-value pairs in dictionary d in the form of dict_items (in Python 2, d. iteritems () returns an iterator object for key-value pairs. Python 3 does not have the iteritems method)
>>> d = {'z': 5, 'x': 1.5, 'y': 3} >>> d.items() dict_items([('z', 5), ('x', 1.5), ('y', 3)]) >>> list(d.items()) [('z', 5), ('x', 1.5), ('y', 3)]
D. keys () returns all keys in dictionary d in the form of dict_keys (in Python 2, d. iterkeys () returns an iterator object for keys. Python 3 does not have this syntax)
>>> d.keys() dict_keys(['z', 'x', 'y']) >>> for x in d.keys(): print(x) z x y
D. pop (x) returns the value corresponding to the given key x and deletes the key-value pair from the dictionary.
>>> d = {'z': 5, 'x': 1.5, 'y': 3} >>> d.pop('x') 1.5 >>> d.pop('x') Traceback (most recent call last): File "<pyshell#92>", line 1, in <module> d.pop('x') KeyError: 'x'
D. popitem () returns and deletes random key-value pairs in dictionary d.
>>> d = {'z': 5, 'x': 1.5, 'y': 3} >>> d.popitem() ('z', 5) >>> d.popitem() ('x', 1.5)
D. setdefault (x, [, y]) returns the value corresponding to the key x in the dictionary d. If the key x does not exist, it returns y and returns x: y is added to the dictionary as a key-value pair. The default value of y is None.
>>> d = {'z': 5, 'x': 1.5, 'y': 3} >>> d.setdefault('x') 1.5 >>> del d['x'] >>> d.setdefault('x','Look!') 'Look!' >>> d {'z': 5, 'x': 'Look!', 'y': 3}
D. update (x) adds all key-value pairs in dictionary x to dictionary d (repeated key-value pairs are replaced by key-value pairs in dictionary x)
>>> d1 = {'x':1, 'y':3} >>> d2 = {'x':2, 'z':1.4} >>> d1.update(d2) >>> d1 {'z': 1.4, 'x': 2, 'y': 3}
D. values () returns all the values in the dictionary in the form of dict_values (d. itervalues () returns the iterator object for all values in dictionary d. Python 3 does not have this syntax)
>>> d1 {'z': 1.4, 'x': 2, 'y': 3} >>> d1.values() dict_values([1.4, 2, 3]) >>> list(d1.values()) [1.4, 2, 3]