A dictionary is another mutable container model and can store any type of object. Each key value of the dictionary (key=>value) pairs with a colon (:) split, each key-value pair is separated by a comma (,), and the entire dictionary is included in curly braces ({}), 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.
1. Create a dictionary
Dict1={"A": 1, "B": 2, "C": "3", "D": 4}dict2={"a": [1,2,3,4], "B": 2, "C": "3", "D": 4}dict3={"a": [1,2,3,4], "B": 2, "(5,6,7,8 ) ":" 3 "," D ": 4}
2. Access to the dictionary
The dictionary is unordered, so it cannot be accessed by the index, only through the key access value.
Dict1["A"]1dict1["B"]2
If the key does not exist, it will be reported Keyerror.
dict1[' E ']traceback (most recent): Python Shell, prompt, line 1KeyError: ' E '
3. Modify the Dictionary
Because the dictionary key is unique, the dictionary can only modify the value that corresponds to the key. If the key does not exist, a new key-value pair is added to the dictionary.
Dict1{' A ': 1, ' C ': ' 3 ', ' B ': 2, ' d ': 4}dict1[' a ']=10dict1[' B ']=20dict1[' e ']=5print dict1{' a ': ten, ' C ': ' 3 ', ' B ': ', ' e ': 5, ' d ': 4}
None can be a dictionary key or value
dict1[' e ']=noneprint dict1{' a ': ten, ' C ': ' 3 ', ' B ': ', ' e ': None, ' d ': 4}dict1[none]=2print dict1{' A ': ten, ' C ': ' 3 ', ' B ': ' E ': None, ' d ': 4, None:2}
Iv. Methods of the dictionary
1. Clear Empty Dictionary
D.clear (), None. Remove all items from D
Dict1={"A": 1, "B": 2, "C": 3, "D": 4}dict1.clear () Print dict1{}
2. Copy Duplicate dictionary
D.copy (), a shallow copy of D
Dict1={"A": 1, "B": 2, "C": 3, "D": 4}test=dict1.copy () print test{' A ': 1, ' C ': 3, ' B ': 2, ' d ': 4} The Copy method of the dictionary is a shallow copy test== Dict1truetest is Dict1false
3. Fromkeys creates a new dictionary with the keys of the elements in the sequence s, and Val is the initial value corresponding to all the keys in the dictionary.
Dict.fromkeys (S[,v]), New dict with the keys from S and values equal to V. V defaults to None.
Dict1.fromkeys ([' A ', ' B '],10) {' A ': ten, ' B ': 10}
4, get gets the value of the specified key
D.get (K[,d]), D[k] if k in D, else D. D defaults to None.
Print Dict1
{' A ': 1, ' C ': 3, ' B ': 2, ' d ': 4}
Dict1.get (' a ')
1
Dict1.get (' B ')
2
The Get method is similar to an Access dictionary, but get is also called a safe value, which means that when a key does not exist, it does not cause the program to crash, but instead returns none.
Dict1.get (' a ') 1dict1.get (' B ') 2dict1.get (' E ')
5, Has_key to determine whether the key exists, return Boolean value
D.has_key (k)-True if D has a key k, else False
Dict1.has_key (' a ') truedict1.has_key (' E ') False
6. Items returns an array of traversed (key, value) tuples as a list
D.items (), List of D ' s (key, value) pairs, as 2-tuples
Dict1.items () [(' A ', 1), (' C ', 3), (' B ', 2), (' d ', 4)]
7. Keys returns a dictionary key in the form of a list
D.keys (), List of D ' s keys
values returns the value of the dictionary in the form of a list
D.values (), List of D ' s values
Dict1.keys ()
[' A ', ' C ', ' B ', ' d ']
Dict1.values ()
[1, 3, 2, 4]
8, Dict1.update (DICT2) update the dictionary dict2 key/value pairs to Dict1
D.update ([E,]**f), None. Update D from Dict/iterable E and F. If e present and has A. Keys () method, Does:for k in e:d[k] = e[k] If e present and lacks. Keys () method, Does:for (K, v) in e:d[k] = V In either case, the is followed By:for k in f:d[k] = F[k
Dict1.update ({"V": 1}) Print dict1{' A ': 1, ' C ': 3, ' B ': 2, ' d ': 4, ' V ': 1}
9, SetDefault and get () are similar, but if the key does not exist in the dictionary, the key will be added and the value will be set to default
D.setdefault (K[,d]), D.get (K,d), also set D[k]=d if K not in D
Dict1.setdefault (' a ') 1dict1.setdefault (' R ') print dict1{' A ': 1, ' C ': 3, ' B ': 2, ' d ': 4, ' R ': None, ' V ': 1}
10, pop Delete the value of the dictionary given the key, the return value is the deleted value
D.pop (K[,d])-V, remove specified key and return the corresponding value. If key is no found, D is returned if given, otherwise keyerror is raised
Dict1={"A": 1, "B": 2, "C": 3, "D": 4}dict1.pop (' a ') 1dict1.pop (' E ') Traceback (most recent call last): Python Shell, prompt 95 , line 1KeyError: ' E '
11, Popitem () randomly delete a pair of keys and values in the dictionary, the return value is a tuple, the elements of the tuple are deleted key value pairs
D.popitem (), (k, v), remove and return some (key, value) pair as a 2-tuple; But raise Keyerror if D is empty.
Dict1.popitem () (' C ', 3) dict1.popitem (' B ', 2)
12, Iterkeys () returns an iterator to the dictionary key
Iteritems () returns an iterator to the dictionary key-value pair
Itervalues () returns an iterator to the dictionary value
These three methods are iterators, which are often used in conjunction with a For loop.
Introduction to Python data structure methods four ———— dictionaries