A diagram in Python--A dictionary
A dictionary is another mutable container model and can store any type of object.
Each key-value pair in the dictionary is key-value with a colon: a split, a comma, a split between each pair, and the entire dictionary is enclosed in curly braces {}, and 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, and the list collection is not possible.
1. Create a dictionary
Variable name = {Dictionary element}
>>> dict = {‘AA‘:‘001‘,‘BB‘:‘002‘,‘CCC‘:3,4:4}>>> dict{‘AA‘: ‘001‘, ‘BB‘: ‘002‘, ‘CCC‘: 3, 4: 4}>>> dict1 = { ‘abc‘: 456 }>>> dict1{‘abc‘: 456}>>> dict2 = { ‘abc‘: 123, 98.6: 37 }>>> dict2{‘abc‘: 123, 98.6: 37}
2. Find (Access dictionary element): Dict[key]
A dictionary cannot access an element as a list by its indexed value, and a dictionary lookup value is a value that is placed in square brackets to access the key. If the data is accessed by a key not in the dictionary, an error is made.
>>> dict{‘AA‘: ‘001‘, ‘BB‘: ‘002‘, ‘CCC‘: 3, 4: 4}>>> dict[‘AA‘]‘001‘>>> dict[4]4>>> dict[‘xxx‘]Traceback (most recent call last): File "<stdin>", line 1, in <module>KeyError: ‘xxx‘
2nd method: The Get () method provided by Dict.
If the key does not exist, it will not error and will not return any information, but you can also specify the return value when it does not exist.
>>> dict{‘AA‘: 1, ‘BB‘: ‘002‘, ‘CCC‘: 3, 4: 4, ‘DDD‘: 889, ‘new‘: ‘新的键值对‘}>>> dict.get(‘BB‘)‘002‘>>> dict.get(‘NO‘)>>> dict.get(‘NO‘,-1)-1>>> dict.get(‘NO‘,‘不存在‘) ‘不存在‘
3. Adding elements
Adding new content to a dictionary means adding new key/value pairs
>>> dict[‘DDD‘] = 889>>> dict{‘AA‘: ‘001‘, ‘BB‘: ‘002‘, ‘CCC‘: 3, 4: 4, ‘DDD‘: 889}>>> dict[‘new‘] = ‘新的键值对‘>>> dict{‘AA‘: ‘001‘, ‘BB‘: ‘002‘, ‘CCC‘: 3, 4: 4, ‘DDD‘: 889, ‘new‘: ‘新的键值对‘}
4. Modifying elements
Since a key can only correspond to one value, the value is placed on a key more than once, and subsequent values are flushed out of the previous value, that is, the value of the element is modified.
Similarly, modifying a nonexistent key will cause an error.
>>> dict[‘AA‘] = 1>>> dict{‘AA‘: 1, ‘BB‘: ‘002‘, ‘CCC‘: 3, 4: 4, ‘DDD‘: 889, ‘new‘: ‘新的键值对‘}
5. Deleting an element
Del Dict[key]
Delete the pair of key values corresponding to key key in the Dict dictionary.
>>> dict{‘AA‘: 1, ‘BB‘: ‘002‘, ‘CCC‘: 3, 4: 4, ‘DDD‘: 889, ‘new‘: ‘新的键值对‘}>>> del dict[‘AA‘]>>> dict{‘BB‘: ‘002‘, ‘CCC‘: 3, 4: 4, ‘DDD‘: 889, ‘new‘: ‘新的键值对‘}
Del can also be used to delete the entire dictionary, and the dictionary will not exist after deletion.
>>> dict2{‘abc‘: 123, 98.6: 37}>>> del dict2>>> dict2Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name ‘dict2‘ is not defined #删除后,字典不存在,访问不存在的对象会报错
Empty dictionary: Dict.clear ()
Just delete all the elements in the dictionary that are key-value pairs, and the dictionary still exists as an empty dictionary.
>>> dict1{‘abc‘: 456}>>> dict1.clear()>>> dict1{}
The order in which the dict is stored inside is not related to the order in which the key is placed.
Using a dictionary, and then finding the data, is fast. Because the realization principle of dict and check Chinese dictionary is the same. Suppose the dictionary contains 10,000 characters, we need to look up a word, one way is to turn the dictionary back from the first page, until we find the word we want, this method is to find the element in the list method, the larger the list, the slower the lookup.
The second method is to search the dictionary's index table (such as the Radicals) for the corresponding page number, and then go directly to the page, find the word. Whichever word you look for, this is very fast and does not slow down as the dictionary size increases.
Dict is the second implementation, given a name, such as ' Michael ', Dict can directly calculate Michael's corresponding storage Score "page number", that is, 95 of the memory address of the number stored, directly out, so the speed is very fast.
As you can guess, this key-value storage method, when put in, must be based on the key to calculate the storage location of value, so that the time can be obtained by key directly to the value.
Compared with list, Dict has the following features:
1. Find and insert the speed is very fast, will not be slow with the increase of key;
2. It takes a lot of memory and a lot of wasted memory.
And the list is the opposite:
1. The time to find and insert increases as the element increases;
2. Small footprint, very little wasted memory.
So, Dict is a way of exchanging space for time.
Python Learning-dictionaries