The tenth day of summing up the python Python beginning to learn what [another data dictionary objects -----] (novice can supervise each other)

       Read all the comments, or have an unexpected delivery. Thank review and supplement each small partners.

Many hands make light ~

        Today's note is to record data objects in python ---- dictionary!

Spoke in front of a list of notes list and tuple of tuples, they are all the same can be used to store objects. list file is modified to support reading and writing, to support tuple of tuples read, can not be modified. But they have common characteristics, the individual element values ​​are stored in lists or tuples, then go through the index to find the corresponding element (index elements) in mode operation.

       So dictionaryt dictionary it? It is what data structure?

This dictionary data structure is called mapping. Value (value) is not particularly dictionary order, but these values ​​are stored in a specific key (key). We can find the corresponding values ​​from the key (key). Key (key) can be numbers, strings or the like tuples.

     It's that understanding, and the Xinhua Dictionary, like, go through the alphabet corresponding word. ~ ~ ~ ~ Is such a mapping means. Not too seriously.

We look at the next creates an empty dictionary:

. 1 dict_1 = {}      # This is an empty dictionary {}, he points with variable dict_1.

We create a data dictionary: below 19, a digital object. Quotes is the string object. Specific did to see your own use, dictionaries are supported ~ ~ ~ ~

. 1 dict_1 = { ' name ' : ' Xiao Hong ' , ' Age ' :. 19, ' iPhone ' : ' 110 ' }     # Create a first type of data dictionary 
2  
. 3  
. 4 dict_2 = { ' Xiao Hong ' : { ' name ' : ' Xiao Hong ' , ' Age ' :. 19, ' iPhone ' : ' 110 ' },    #Create a second type of dictionary data, but also can be created oh 
. 5            ' Xiao On Jun ' : { ' name ' : ' Xiao On Jun ' , ' Age ' : 16, ' iPhone ' : ' 117 ' }}

        We found that: create a second dictionary, its key (key), the corresponding value (value) is another class dictionary. In fact, the key (key) dictionary objects also support many types of objects,

The most common type is numeric and string as a key (key), because the target of the search is the highest efficiency.

        I believe you will have to create such a dictionary mapping structure in accordance with the above structure. Next we create a new dictionary, add, modify, delete operations.

Empty dictionary, add objects:

. 1 dict_1 = {}    # Create an empty dictionary 
2  
. 3 dict_1 [ ' name ' ] = ' Xiao '      # variable name [key] = [value], the direct assignment Add the objects 
. 4 dict_1 [1.34] = ' ABC ' 
. 5 dict_1 [100] = ' Holle Word ' 
. 6  
. 7  Print (dict_1)   # view the effect of adding the object

 

. 1 dict_1 = {}    # Create an empty dictionary 
2  
. 3 dict_1 [ ' name ' ] = ' Xiao '      # variable name [key] = [value], adding objects direct assignment manner 
. 4 dict_1 [1.34] = ' ABC ' 
. 5  Print (dict_1)                # view the effect of adding the object 
. 6  
. 7 dict_1 [ ' name ' ] = ' HHH '       # a 'name': [ 'Xiao' ] read: 'name': [ 'HHH'] 
. 8  Print (dict_1)                # View modified object effect, do comparison

        我们看到了添加和修改,我要搞清楚一个概念,如果在我们赋值操作的时候,键(key)在字典中是不存在的,呢么就是添加对象;如果在我们赋值的时候,键(key)是本身字典就有同名的键(key),那就是修改。

如果说我们添加的时候不知道里面有没有同名的键(key),我们可以这样检查一下键(key)是不是在字典中已经存在:

1 dict_1 = {'name':'Xiao Hong','age':19,'iphone':'110'}
2 
3 print('name' in dict_1)   #要查询的格式    键 in关键字 字典对象(变量名)
4 print('hhh' in dict_1)    #返回True就是存在,返回的False则是不存在

 

 当然也可以使用这个方法:

has_key()     可以检查当前字典中键是否存在,但是!!!python3中没有这个方法啦,所以不再举例了,用第一种 in关键字就好了.。

 

我们再看下删除字典中的一个元素,代码怎么实现:

1 dict_1 = {'name':'Xiao Hong','age':19,'iphone':'110'}
2 print(dict_1)         #删除之前
3 
4 del dict_1['age']    #del关键字
5 print(dict_1)        #删除之后

也可以这样删除字典中的元素:

1 dict_1 = {'name':'Xiao Hong','age':19,'iphone':'110'}
2 print(dict_1)         #删除之前
3 
4 a = dict_1.pop('name')    #pop()方法,删除字典元素的时候,将键对应的值返回给了变量。
5 print(dict_1)             #删除之后
6 print(a)                  #我们看下变量a有没有指向删除的值 'Xiao Hong'

我看的pop()方法,删除字典元素的时候,将键对应的值返回给了变量。

 

有时候我们会遍历字典中的键:

使用 keys() 方法:

1 dict_1 = {'name':'Xiao Hong',
2           'age':'19',
3           'iphone':'110'}            #我们创建的字典对象
4 
5 for key in dict_1.keys():         #遍历字典中的键key
6     print(key,end= ' ')    # end=' '意思是末尾不换行,加空格。

 

 

同样的,我们也可以遍历字典中的所有值:

使用values()  方法:

1 dict_1 = {'name':'Xiao Hong',
2           'age':'19',
3           'iphone':'110'}            #我们创建的字典对象
4 
5 for value in dict_1.values():         #遍历字典中的值value
6     print(value,end= ' ')    # end=' '意思是末尾不换行,加空格。

 

 有时候我们会同时遍历字典中的键和值,这个需要用到字典中的items() 方法:

 items() 方法:  它可以同时获取key和value  我们看下代码的实现:

1 dict_1 = {'name':'Xiao Hong',
2           'age':'19',
3           'iphone':'110'}            #我们创建的字典对象
4 
5 for key,value in dict_1.items():   #用 items()函数,同时遍历键和值
6     print(key,value,end= '')              #打印键和值,end=' '意思是末尾不换行,加空格。

 

 
1 dict_1 = {'name':'Xiao Hong',
2           'age':'19',
3           'iphone':'110'}            #我们创建的字典对象
4 
5 print(dict_1.get('name'))    #我们可以使用get()方法获取字典的值

 

字典的合并,就是将两个字典合并在一起

update()   需要注意的是:如果被合并的字典中包含已经存在的键值对,会覆盖原有的键值对。

 1 dict_1 = {'name_1':'Xiao Hong',
 2           'age':'19',
 3           'iphone':'110'}            #第一个字典
 4 
 5 print(dict_1)                        #合并之前
 6 dict_2 = {'name_2':'Xiao JUN',
 7           'age':'19',
 8           'iphone':'110'}            #第二个字典
 9 
10 dict_1.update(dict_2)               #将dict_2合并在dict_1字典中。
11 print(dict_1)                        #合并之后的dict_1字典,注意重复的键值对会被覆盖掉

 

 清空字典用clear() 方法:

1 dict_1 = {'name_1':'Xiao Hong',
2           'age':'19',
3           'iphone':'110'}            #创建一个字典
4 
5 print(dict_1)                        #清空之前
6 print(dict_1.clear())                #清空并打印结果

 

字典还有很多方法,后面的笔记,遇见不同的场景,处理字典的时候我们在详细分析记录。

 

Guess you like

Origin www.cnblogs.com/woshidaliua/p/11279233.html