The difference between dictionaries and images, maps, and mirrors

Mapping is an associated container type, which is used to store the mapping relationship between objects and objects.
Here I will confuse the difference between image, mapping and mirroring. Baidu has checked a lot, and the summary is probably: =
>①Image : noun, It is mainly used in system packaging and boot disk. The image file is basically in WIM (Microsoft Windows Imaging Format) format, which is a file that combines data and programs. It can be understood as a compressed file similar to ZIP and RAR.
=>②Mapping : verb, which describes a relationship from A to B, and B is called the image of A under this mapping; for example, a memory-mapped file, which exists in RAM memory, is an image of a physical file on disk.
=>③Mirroring : Mirroring is like looking in a mirror. There are many formats of mirror images. The mirror image we generally refer to is to make a ghost image for the system, so that a complete system can be restored very conveniently in a short time. An image can be said to be a kind of file, such as iso and gho, both belong to the image file, and the image file can be directly burned to a CD, or opened with a virtual CD-ROM. (I first came into contact with mirroring and used the NPM package mirror address, which can quickly switch the server address when installing the package, so I will confuse /xk when encountering some words).

Closer to home!
A dictionary (dict), also known as a hash table, is the only mapping type in Python and is an associative container used to store key-value pairs (keys are mapped to values). Each key-value (key => value) pair of the dictionary is separated by a colon (:), and each pair is separated by a comma (,). The entire dictionary is included in curly braces ({}), and the format is: dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}

1. Access the dictionary:

>>> dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}
>>> print(dict['messi'])
169
>>> print(dict['xavi'])
170
>>> 

2. Modify the dictionary

The content of the python dictionary can be modified, added, and deleted.
①Modify the value corresponding to the existing key in the dictionary:

>>> dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}
>>> dict['messi'] = '182'
>>> dict
{'messi': '182', 'xavi': '170', 'Iniesta': '168'}
>>> 

② Add new keys and values ​​to the dictionary:

>>> dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}
>>> dict['Tom'] = '175'
>>> dict
{'messi': '169', 'xavi': '170', 'Iniesta': '168', 'Tom': '175'}
>>> 

③ Use del dictionary name [key] to delete a bunch of keys and values ​​in the dictionary, use the clear statement to clear the dictionary and make it an empty dictionary, and use del dictionary name to delete the entire dictionary:

>>> dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}
>>> del dict['messi']
>>> dict
{'xavi': '170', 'Iniesta': '168'}
>>> dict.clear()
>>> dict
{}
>>> del dict
>>> dict	# 因为del语句将字典完全删除,所以再次访问时会报错
<class 'dict'>
>>> 

Three, the characteristics of the dictionary key

The keys of the dictionary must be unique. If the dictionary is defined to use multiple keys, the system will only remember the last set of keys and values; the value of the dictionary can be of any data type, but the key must be an immutable data type , such as strings, numbers, or tuples, not lists.

Fourth, the built-in functions contained in the dictionary

len(dict) calculates the number of dictionary elements, that is, the total number of keys

>>> dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}
>>> len(dict)
3

dict.keys() returns all the keys in the dictionary

>>> dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}
>>> dict.keys()
dict_keys(['messi', 'xavi', 'Iniesta'])
>>> 

dict.value() returns all the values ​​in the dictionary

>>> dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}
>>> dict.values()
dict_values(['169', '170', '168'])
>>> 

dict.update(dict2) Update the keys and values ​​of the dictionary dict2 to the dictionary dict1

>>> dict = {'messi': '169', 'xavi': '170', 'Iniesta': '168'}
>>> dict2 = {'Tom':'175'}
>>> dict.update(dict2)
>>> dict
{'messi': '169', 'xavi': '170', 'Iniesta': '168', 'Tom': '175'}
>>> 

Guess you like

Origin blog.csdn.net/weixin_42676530/article/details/105727596