{}
表示dict_name={}
:
隔开,
分隔开。dict_name[key]=new_value
key
来修改对应的值dict_name = {1:1,'name':'xwh',(1,):(2,3)} dict_name[3.2] = 2 print(dict_name)
dict_name[key]
}
key
获取对应的值,当key
不存在时会报错。dict_name.get(key[,value])
key
获取字典的值,如果key
不存在,则返回None
或者value
。dict_name = {1:1,'name':'xwh',(1,):(2,3)} print(dict_name[(1,)]) print(dict_name.get((1,)),'\n',dict_name.get(0,'vava'))
dict_name.pop(key)
key
删除对应的value
。dict_name = {1:1,'name':'xwh',(1,):(2,3)} dict_name.pop((1,)) print(dict_name)
dict_name[new_key]=new_value
key
和对应的value
dict_name = {1:1,'name':'xwh',(1,):(2,3)} dict_name[3.2] = 1 dict_name['sex'] = 'mam' print(dict_name)
dict_name.setdefault(key,default)
key
设置默认值。当指定的key
不存在时,设置的默认值。也相当于创建了一个键值对。person = {'name':'jack','sex':'nan'} person.setdefault('age',23) print(person)
dict_name.items()
person = {"name":"xwh","sex":"男","age":24} for k,v in person.items(): print(f"key:{k}",f"value:{v}")
dict_name.keys()
person = {"name":"xwh","sex":"男","age":24} for k in person.keys(): print(f"key:{k}")
dict_name.values()
person = {"name":"xwh","sex":"男","age":24} for v in person.values(): print(f"value:{v}")
dict_name.update(dict_name)
dic1 = {'name':'xwh','age':23} dic2 = {'sex':'nan'} dic1.update(dic2) print(dic1)
dict(dict_name,**dict_name)
dic1 = {'name':'xwh','age':23} dic2 = {'sex':'nan'} dic = dict(dic1,**dic2) print(dic)
len(dict_name)
dict_name = {1:1,'name':'xwh',(1,):(2,3)} print(len(dict_name))
list(dict_name)|set(dict_name)|tuple(dict_name)
dict_name = {1:1,'name':'xwh',(1,):(2,3)} print(list(dict_name))