python全棧筆記-day03-dictionary
阿新 • • 發佈:2018-12-11
# dic = {'k1':'v1','k2':'v2','k3':'v3'} # print(dic) # #迴圈遍歷出所有的key # for k in dic: # print(k) # #迴圈遍歷出所有的value # for k in dic: # print(dic[k]) # #迴圈遍歷出所有的key和value # for k in dic: # print(k,dic[k]) #新增一個鍵值對 # dic['k4'] = 'v4' # 刪除一個鍵值對 # dic.pop('k1') # 刪除鍵’k5‘對應的值,若不存在,則不報錯,並返回None # print(dic.get('k5')) # 獲取’k2‘對應的值 # print(dic.get('k2')) # 獲取鍵’k6‘對應的值,若不存在,則不報錯,並返回None # print(dic.get('k6')) # dic2 = {'k1':'v111','a':'b'} # dic2.update(dic) # # print(dic2) # print(dic) # s = 'kjklsj' # print(s.upper()) # print(s) # lis = [['k',['qwe',20,{'k1':['tt',3,'1']},89],'ab']] #將tt用兩種方法變成大寫 # lis[0][1][2]['k1'][0] = lis[0][1][2]['k1'][0].upper()#法一 # lis[0][1][2]['k1'][0] = 'TT'#法二 #將列表中數字3變成字串'100'兩種方法 # lis[0][1][2]['k1'][1] = '100'法一 # lis[0][1][2]['k1'] = ['tt','100','1']法二 #將列表中字串’1‘變成數字101兩種方法 # lis[0][1][2]['k1'][2] = 101法一 # lis[0][1][2]['k1'] = ['tt','100',101]法二 # print(lis) li = [1,2,3,'a','b,',4,'c'] dic = {} dic.setdefault('k1',[]) dic['k1'] = li[1::2] for i in li[1::2]: dic['k1'].append(i) print(dic)