Python----addition, deletion, modification, and nesting of dictionaries

#dictionary 
#One of the basic data types, {} stores data in the form of key-value pairs.
# dic={'name':'Laonanhai',
# 'age':45,
# 'name_list':['Reverse textbook','Junjun',....]
# }
# In the form of key:value, map , which stores a large amount of relational data.
# Classification of data types:
# Variable (unhashable): list,dict,set,list=[11,222,333] #Immutable (hashable
): str,tuple,int,bool name='alex'
# Container class:
# Data type of container class: list, tuple, dict, l1=[11,True,'alex',[22,33,44]]
# Data type of non-container class: str, int, bool. s1 ='[11,22,33]'
# The key of the dictionary must be an immutable data type and is unique.
# Hash algorithm: The key of the dictionary is converted into a number through the hash table, and the number is queried by binary search
# The value of the dictionary can be Is any data type
# Dictionary query speed is very fast, storing a large amount of relational data
# The dictionary is unordered before python3.5 includes 3.5, but it is ordered after 3.6 #The
key is unique
# dic={
# 'name':'alex', #automatically overwrite when there are two
# 'name' :'WuSir',
# }
# print(dic) #Keys
must be immutable hashable data types
# dic={'name':'old boy','age':'56','hobby': 'women'}
#Add
# dic['sex']='LaddyBoy'
# dic['name']='Alex' #Overwrite if there is, add if not
# dic.setdefault('sex','Laddyboy') # If there is, it will not change, if not, it will be added.
# dic.setdefault('name','alex')
# print(dic)

#delete
# print(dic.pop('age')) #There is a return value, which returns the value corresponding to the key
# print(dic.pop ('age1','There is no such key....'))#The return value can be reassigned
# print(dic)

# dic.





#del
#1, delete the entire dictionary
# del dic # The dictionary after deletion does not exist, and an error will be reported if it is printed again
# print(dic)
#2, delete the key-value pair according to the key
# del dic['name']
# print(dic )

#Change
# dic['name']='Alex' #If there is, then overwrite, if not, add
# #update Update of two dictionaries
# dic={'name':'jin','age':18,'sex ':'male'}
# dic2={'name':'alex','weight':75}
# dic2.update(dic) #Add all key-value pairs in dic to dic2, dic remains unchanged
# print(dic) #{'name': 'jin', 'age': 18, 'sex': 'male'}
# print(dic2) #{'age': 18, 'name': 'jin', 'weight': 75, 'sex': 'male'}

#Check
#1,dic['name']
# print(dic['name'])
#
# #2.dic.get('name')
# print(dic.get('name'))
# print(dic.get('name1')) #It will return none when the get query is not available operate on him
# print(dic.get('name1','sb没有此键'))

#for循环查询
#dic.key(), dic.values(), dic.items() #类似于list但不是list的类型。
# print(dic.keys(),type(dic.keys()))
# for key in dic.keys():
# print(key)
# l_key=list(dic.keys())
# print(l_key)
# for value in dic.values():
# print(value)
# l_value=list(dic.values())
# print(l_value)
# print(dic.items())

#分别赋值概念
# a,b=1,2
# c,d=('alex','34')
# e,f=['alex','34']
#有如下a=1 b=3,用一行代码将a,b值互换。
# a=1
# b=3
# a,b=b,a
# print(a,b)
# print(dic.items())
#
# for i in [22,33,44]:
# print(i)
# for i in [(11,22),(33,44),(55,66)]:
# print(i)
# # k,v=('name','老男孩')
# for k,v in dic.items():
# print(k,v)

#字典的嵌套
dic = {
'name_list':['碗蓉','俊俊','爽妹'],
'status':None,
'personal_msg':{
'name':'反面教材',
'age':25,
'hobby_list':['抽烟','喝酒','烫头'],
}

}
# 1,给dic添加一个键值对,学校:老男孩
# dic['学校']='老男孩'
# print(dic)
# 2,将status对应的值改成True
#第一种方法:
# dic3={'status':'True'}
# dic.update(dic3)
# #第二种方法:
# # dic['status']='True'
# print(dic)
# 3,给name_list对应的列表追加一个元素:平平
# dic['name_list'].append('平平')
# print(dic)
# 4,给personal_msg对应的字典添加一个键值对sex:男
# dic['personal_msg']['esx']='男'
# print(dic)
# 5,将hobby_list 对应的列表中的烫头改成唱歌。
dic['personal_msg']['hobby_list'][-1]='唱歌'
print(dic)







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325253812&siteId=291194637