Python第一天——入门Python(4)字典的常用操作
# dic={[1,2,3]:'123'} #可变类型不能当做字典的key,value可以使用任意类型 # dic={(2,3,4):'123'} # print (dic[(2,3,4)]) #元组可以作为字典的key # dic={1:'a',1:'b'} # print (dic[(1)]) #重复的key可以定义但是永远取最后一个key的value
# dic={1:'a',2:'b',3:'c'} #查看key值 # print (dic[2]) # dic={1:'a',2:'b',3:'c'} # print (dic) # dic[1]='Chares_Lee' #修改key值 # print (dic) # dic={1:'a',2:'b',3:'c'} #删除key # del dic[1] # print (dic) # dic={1:'a',2:'b',3:'c'} # dic['key']='value' #增加key值 # print (dic) # dic={1:'a',2:'b',3:'c'} # for i in dic: # print(i) #遍历列表中的key值 # print (i,dic[i]) #遍历列表中的kye:value值
# dic={1:'a',2:'b',3:'c'} # dic1={9:'x',8:'y',7:'z',1:'a'} # dic.update(dic1) # print (dic) #用dec1更新dic 在dic中有的则覆盖,没有的则添加进dic中 # dic={1:'a',2:'b',3:'c'} # dic.setdefault('sex','female') #设置默认值,类似于dic['sex']='female' # print (dic) # dic={1:'a',2:'b',3:'c'} # dic.setdefault('hobby',[]).append('read') #类似于dic['hobby'].append('read') # print (dic)
# dic={1:'a',2:'b',3:'c'} # dic.pop(1) #pop删除 # print (dic) # dic={1:'a',2:'b',3:'c'} # print (dic.pop('dalsj','找不到删除的值')) #设置默认的值,在pop的值不存在的时候则不会报错
dic={1:'a',2:'b',3:'c'} dic.clear() #清除字典 print (dic)
# dic={1:'a',2:'b',3:'c'} # print (dic.popitem()) # print (dic) #随机删除字典中的kye:value值并返回一个元组
相关推荐
非攻 2020-10-25
youandme 2020-05-11
haokele 2020-04-23
paopao00 2020-04-07
zhangpan 2020-02-23
wyqwilliam 2020-02-14
Winterto0 2020-01-13
JackLang 2019-12-28
LczPtr 2019-12-24
georgeandgeorge 2019-12-09
wordmhg 2019-11-01
小炮集揣 2019-05-03
ciqingloveless 2019-06-30
yuan00yu 2019-06-28
Kwong 2019-06-27
远哥的小迷弟 2019-03-08