1. 程式人生 > >python開發基礎:字典操作

python開發基礎:字典操作

ems append code 如果 pri 並且 常用操作 item set

一,字典操作

  1 #!/usr/bin/env python
  2 #_*_coding:utf-8_*_
  3 
  4 
  5 # 常用操作:
  6 #
  7 #     存/取
  8 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘}
  9 # print(info_dic[‘name11111111‘]) #沒有這個key,報錯
 10 # print(info_dic.get(‘name1‘,None)) #最好用get取值,沒有的情況下返回None,可以自己定義返回的這個None
 11 
 12 #pop:key存在則彈出值,不存在則返回默認值,如果沒有默認值則報錯
13 # print(info_dic.pop(‘name‘,None)) 14 # print(info_dic) 15 16 # print(info_dic.popitem()) #隨機刪除 17 # print(info_dic) 18 19 # info_dic[‘level‘]=10 #添加 20 # print(info_dic) 21 22 # 23 #     刪除 24 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 25 # info_dic.pop() 26 # info_dic.popitem()
27 # del info_dic[‘name‘] 28 29 30 # 31 #     鍵s,值s,鍵值對 32 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 33 # print(info_dic.keys()) 34 # print(info_dic.values()) 35 # print(info_dic.items()) 36 37 # for k in info_dic: 38 # # print(k,info_dic[k]) 39 # print(k) 40 41 # print(‘========>‘)
42 # for k in info_dic.keys(): 43 # print(k) 44 45 # for val in info_dic.values(): 46 # print(val) 47 48 # for k,v in info_dic.items(): #k,v=(‘name‘, ‘egon‘) 49 # print(k,v) 50 51 52 53 #     長度 54 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 55 # print(len(info_dic)) 56 # 57 #     循環 58 # 59 #     包含in 默認比對的是KEY 60 61 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 62 # print(‘name‘ in info_dic) 63 # print(‘name‘ in info_dic.keys()) 64 # print(‘egon‘ in info_dic.values()) 65 # print((‘name‘,‘egon‘) in info_dic.items()) 66 67 68 #掌握 69 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 70 # info_dic.update({‘a‘:1,‘name‘:‘Egon‘}) #有的話就是覆蓋,沒有的話就是添加 71 # print(info_dic) 72 73 # info_dic[‘hobbies‘]=[] 74 # info_dic[‘hobbies‘].append(‘study‘) 75 # info_dic[‘hobbies‘].append(‘read‘) 76 # print(info_dic) 77 78 #setdefault:key不存在則設置默認值,並且將默認值添加到values中 79 #key存在則不設置默認,並且返回已經有的值 80 81 # info_dic.setdefault(‘hobbies‘,[1,2]) 82 # print(info_dic) 83 # info_dic.setdefault(‘hobbies‘,[1,2,3,4,5]) 84 # print(info_dic) 85 86 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 87 88 # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘]} 89 # info_dic.setdefault(‘hobbies‘,[]).append(‘study‘) 90 # print(info_dic) 91 92 # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘,‘read‘]} 93 # info_dic.setdefault(‘hobbies‘,[]).append(‘read‘) 94 # print(info_dic) 95 96 # {‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘,‘hobbies‘:[‘study‘,‘read‘,‘sleep‘]} 97 # info_dic.setdefault(‘hobbies‘,[]).append(‘sleep‘) 98 # info_dic.setdefault(‘hobbies‘,[]).append(‘read‘) 99 # l=info_dic.setdefault(‘hobbies‘,[]) 100 # print(l,id(l)) 101 # print(info_dic,id(info_dic[‘hobbies‘])) 102 103 # info_dic.setdefault(‘hobbies‘,‘a‘) #都可以,id都一樣 104 # info_dic.setdefault(‘hobbies‘,{1}) 105 # # info_dic.setdefault(‘hobbies‘,(2)) 106 # l=info_dic.setdefault(‘hobbies‘,{}) 107 # print(l,id(l)) 108 # print(info_dic,id(info_dic[‘hobbies‘])) 109 110 111 #了解 112 # d=info_dic.copy() 113 # print(d) 114 # info_dic.clear() 115 # print(info_dic) 116 117 # info_dic={‘name‘:‘egon‘,‘age‘:18,‘sex‘:‘male‘} 118 # d=info_dic.fromkeys((‘name‘,‘age‘,‘sex‘),None) #格式化,將values改成None,None可以是任何 119 # print(d) 120 # d1=dict.fromkeys((‘name‘,‘age‘,‘sex‘),None) 121 # d2=dict.fromkeys((‘name‘,‘age‘,‘sex‘),(‘egon‘,18,‘male‘)) 122 # print(d1) 123 # print(d2) 124 # 125 # info=dict(name=‘egon‘,age=18,sex=‘male‘) #字典寫法 126 # print(info) 127 128 # info=dict([(‘name‘,‘egon‘),(‘age‘,18)]) #字典寫法 129 # print(info)

python開發基礎:字典操作