一、
鍵與值用冒號":"分開;
二、項與項用逗號","分開;
1.key:value結構
2.key必須可hash、且必須爲不可變數據類型、必須惟一(數字、字符串、元組)
3.value可存聽任意多個值、可修改、能夠不惟一
4.無序
1 # a.直接用 {} 建立,key:value對應 2 >>> skills = {"s1":"python","s2":"linux","s3":"html/css","s4":"JavaScript"} 3 >>> skills 4 {'s1': 'python', 's2': 'linux', 's3': 'html/css', 's4': 'JavaScript'} 5 6 # b.用dict轉換 7 >>> skills_1 = dict(s1="python",s2="linux") 8 >>> skills_1 9 {'s1': 'python', 's2': 'linux'} 10 >>> skills_1 = dict("s1"="python","s2"="linux") # 不能這樣建立,自己有歧義,先後都是兩個字符串 11 SyntaxError: keyword can't be an expression 12 13 # 將自己就是字典轉換成dict 14 >>> skills_2 = dict({"s1":"python","s2":"linux"}) 15 >>> skills_2 16 {'s1': 'python', 's2': 'linux'} 17 18 # 將形如key-value結構的轉換爲字典 19 >>> skills_3 = dict((["s1","python"],["s2","linux"])) 20 >>> skills_3 21 {'s1': 'python', 's2': 'linux'} 22 >>> skills_3 = dict([["s1","python"],["s2","linux"]]) 23 >>> skills_3 24 {'s1': 'python', 's2': 'linux'} 25 >>> skills_3 = dict([("s1","python"),("s2","linux")]) 26 >>> skills_3 27 {'s1': 'python', 's2': 'linux'} 28 29 # key 爲元組的字典 30 >>> skills_3 = dict([[(1,2),"python"],[(21,21),"linux"]]) 31 >>> skills_3 32 {(1, 2): 'python', (21, 21): 'linux'} 33 34 35 給多個鍵賦相同的值 36 >>> dic = {}.fromkeys(['s1','s2'],['python','linux']) 37 >>> dic 38 {'s1': ['python', 'linux'], 's2': ['python', 'linux']} 39 dic1 = {}.fromkeys(['s1','s2'],['python'],['linux']) #最多兩個變量,多了報錯 40 Traceback (most recent call last): # 第二個變量是爲共用的,當爲可變類型時,能夠修改添加,修改, 41 File "<pyshell#71>", line 1, in <module> 42 dic1 = {}.fromkeys(['s1','s2'],['python'],['linux']) 43 TypeError: fromkeys expected at most 2 arguments, got 3 44 >>> dic['s1'].append(1) 45 >>> dic 46 {'s1': ['python', 'linux', 1], 's2': ['python', 'linux', 1]} 47 >>> dic['s1'].remove('python') 48 >>> dic 49 {'s1': ['linux', 1, 1], 's2': ['linux', 1, 1]}
字典的常見操做css
1 鍵、值、鍵值對 2 dic.keys() 返回一個包含字典全部KEY的列表; 3 dic.values() 返回一個包含字典全部value的列表; 4 dic.items() 返回一個包含全部(鍵,值)元祖的列表; 5 6 >>> skills 7 {'s1': 'python', 's2': 'linux', 's3': 'html/css', 's4': 'JavaScript'} 8 >>> skills.keys 9 <built-in method keys of dict object at 0x00000209A130DCF0> 10 >>> skills.keys() 11 dict_keys(['s1', 's2', 's3', 's4']) 12 >>> skills.values() 13 dict_values(['python', 'linux', 'html/css', 'JavaScript']) 14 >>> skills.items() 15 dict_items([('s1', 'python'), ('s2', 'linux'), ('s3', 'html/css'), ('s4', 'JavaScript')]) 16 17 新增 18 dic['new_key'] = 'new_value' 19 dic.setdefault(key,None) 若是字典中不存在Key鍵,由 dic[key] = default 爲它賦值,這裏defalut=None,實際建立須要填value 20 21 刪除 22 dic.pop(key[,default]) 和get方法類似。若是字典中存在key,刪除並返回key對應的vuale;若是key不存在,且沒有給出default的值,則引起keyerror異常; 23 dic.clear() 刪除字典中的全部項或元素; 24 >>> skills.pop('s4') 25 'JavaScript' 26 >>> skills 27 {'s1': 'python', 's2': 'linux', 's3': 'docker'} 28 >>> skills 29 {} 30 31 修改 32 dic['key'] = 'new_values' 若是key在字典中存在,'new_value'將會替代原來的value值,不存在則建立 33 dic.update(dic2) 將字典dic2的鍵值對添加到字典dic中 34 35 查看 36 dic['key'] 返回字典中key對應的值,若key不存在字典中,則報錯; 37 dict.get(key,default = None) 返回字典中key對應的值,若key不存在字典中,則返回default的值(default默認爲None) 38 39 40 循環(3) 41 a. for i in dic.keys() 等同於 for i in dic 42 >>> for k in skills.keys():print(k) 43 s1 44 s2 45 s3 46 s4 47 >>> for k in skills:print(k) 48 s1 49 s2 50 s3 51 s4 52 53 b. for k,v in dic.items() 54 55 >>> for k,v in skills.items():print(k,v) 56 s1 python 57 s2 linux 58 s3 docker 59 s4 JavaScript 60 c. 61 >>> for k,v in enumerate(skills):print(k,v) 62 63 0 s1 64 1 s2 65 2 s3 66 3 s4 67 >>> for k,v,j in enumerate(skills):print(k,v,j) 68 69 Traceback (most recent call last): 70 File "<pyshell#48>", line 1, in <module> 71 for k,v,j in enumerate(skills):print(k,v,j) 72 ValueError: not enough values to unpack (expected 3, got 2) 73 74 75 長度 76 len(dic)