We learned about Python's lists and tuples earlier. If you study the previous content carefully and do some training through relevant exercises, then the next learning will come naturally. We must understand one thing in Python. Python is an interpreted language, so it will provide many user-friendly tools for us to use. In the next few sections, we will learn a new data type - dictionary .
Dictionaries are different from lists and tuples. What is stored in a dictionary is a set of data. That is to say, each data in the dictionary contains two parts. You can understand it this way. A student name is stored in the dictionary, and each student's name is stored in the dictionary. Each name corresponds to a student number. We can understand the student number as a 'key' and the name as a 'value'.
Here are some features of dictionaries:
1) Dictionaries have no order and are an unordered collection of arbitrary objects.
2) The key of the dictionary is unique and cannot appear multiple times. When it appears multiple times, the last value is taken.
3) Keys are immutable.
4) Elements in the dictionary can be added or deleted.
5) Because there is no order, there is no index.
Through the example of the student's name, it has been mentioned that each element of the dictionary contains 2 parts. They are ' key ' and ' value '. The key and the value are separated by ' : ', and the two elements are separated by ' , 'Separate.
Its grammatical form is:
my_dict={'key':'value','key1'='value1'....}
Among them, my_dict is the dictionary we want to create, key is the key, and value is the value corresponding to the key. They can be any number type.
The way to create an empty dictionary is:
>>>my_dict={}#Create directly >>>my_dict{}>>>type(my_dict)#Look at its type <class'dict'>
Let's create a dictionary. There are 5 student numbers in the dictionary, namely 1001, 1002, 1003, 1004, and 1005. The names corresponding to each student number are 'Li Hua', 'Zhang San', 'Xiaoxue', and 'Xiao Zhang'. ', 'Xiao Ming'.
>>>my_dict={1001:'Li Hua',1002:'Zhang San',1003:'Xiaoxue',1004:'Xiao Zhang',1005:'Xiao Ming'}>>>my_dict#Enter the name in interactive mode It is a direct access output {1001:'Li Hua',1002:'Zhang San',1003:'Xiao Xue',1004:'Xiao Zhang',1005:'Xiao Ming'}
The form of the dictionary is roughly like this. Each element corresponds to two parts, the front one is the 'key', and the back part is the 'value'.
It should be noted that the key cannot be modified, but the value can be changed, so the key must be an immutable type of data.
Accessing an element in a dictionary generally involves accessing its key to obtain its corresponding value.
Continuing with the dictionary created above, we access its values:
>>>my_dict={1001:'Li Hua',1002:'Zhang San',1003:'Xiaoxue',1004:'Xiao Zhang',1005:'Xiao Ming'}>>>my_dict[1001]#The access key is The value of 1001 is 'Li Hua' >>> my_dict [1005] # The access key is the value of 1005 ' Xiao Ming ' >>> my_dict [1006] # The access key is the value of 1006. At this time, there is no 1006 in the dictionary, so an error is reported. Traceback(mostrecentcalllast):File<stdin>,line1,in<module>KeyError:1006
Above we learned that an error will occur when we access a non-existent key. We can use a judgment to determine whether the corresponding key exists in the dictionary.
To determine whether to save or not, we use in and not in .
Use in:
>>>my_dict={1001:'Li Hua',1002:'Zhang San',1003:'Xiaoxue',1004:'Xiao Zhang',1005:'Xiao Ming'}>>>if1001inmy_dict:#If 1001 exists in my_dict This key executes the following statement print(my_dict[1001])>>>>Li Hua
Use not in:
>>>my_dict={1001:'Li Hua',1002:'Zhang San',1003:'Xiaoxue',1004:'Xiao Zhang',1005:'Xiao Ming'}>>>if1006notinmy_dict:#If my_dict exists For the key 1006, execute the following statement print('The key 1006 does not exist')>>>The key 1006 does not exist.
Simply using in and not in can ensure that we reduce the occurrence of errors when accessing. It should be noted that when operating in and not in, string comparison needs to be case-sensitive.
Because the dictionary is mutable, we can perform operations such as additions, deletions, and modifications. The corresponding grammatical forms are:
my_dict['newkey']='newvalue'>>>my_dict={1001:'Li Hua',1002:'Zhang San',1003:'Xiaoxue',1004:'Xiao Zhang',1005:'Xiao Ming'}> >>my_dict[1006]='Xiao Li'#Add key 1006, corresponding value 'Xiao Li'>>>my_dict{1001:'Li Hua',1002:'Zhang San',1003:'Xiao Xue',1004:' Xiao Zhang',1005:'Xiao Ming',1006:'Xiao Li'}
Adding elements is done directly through key-value pairs.
To delete elements, we still delete them through the del statement. What is deleted is an entire pair of elements, including the key and value.
Syntax format:
delmy_list['key']
>>>my_dict{1001:'Li Hua',1002:'Zhang San',1003:'Xiaoxue',1004:'Xiao Zhang',1005:'Xiao Ming',1006:'Xiao Li'}>>>delmy_dict[ 1001]#Delete a group of elements with the key 1001>>>delmy_dict[1002]#Delete a group of elements with the key 1002>>>my_dict{1003:'Xiaoxue',1004:'Xiao Zhang',1005:'Xiao Ming' ,1006:'Xiao Li'}
Modifying an element is equivalent to directly overwriting the existing element. Its format is similar to that of adding an element.
The format is:
my_dict['key']='newvalue'>>>my_dict{1003:'Xiaoxue',1004:'Xiao Zhang',1005:'Xiao Ming',1006:'Xiao Li'}>>>my_dict[1003]=' Xiaotian'#Change the value of key 1003 to 'Xiaotian'>>>my_dict{1003:'Xiaotian',1004:'Xiao Zhang',1005:'Xiao Ming',1006:'Xiao Li'}
In this section, we first understand the basic operations of dictionaries, and in the next section we will learn some dictionary-related methods.