Python字典是另一種可變容器模型(無序),且可存儲任意類型對象,如字符串、數字、元組等其他容器模型。本文章主要介紹Python中字典(Dict)的詳解操作方法,包含創建、訪問、刪除、其它操作等,需要的朋友可以參考下。
字典由鍵和對應值成對組成。字典也被稱作關聯數組或哈希表。基本語法如下:
1.創建字典
>>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'} 技巧: 字典中包含列表:dict={'yangrong':['23','IT'],"xiaohei":['22','dota']} 字典中包含字典:dict={'yangrong':{"age":"23","job":"IT"},"xiaohei":{"'age':'22','job':'dota'"}} 注意: 每個鍵與值用冒號隔開(:),每對用逗號,每對用逗號分割,整體放在花括號中({})。 鍵必須獨一無二,但值則不必。
2.訪問字典里的值
>>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'} >>> print(dict['ob1']) computer 如果用字典里沒有的鍵訪問數據,會輸出錯誤如下: >>> print(dict['ob4']) Traceback (most recent call last): File "", line 1, in print(dict['ob4']) 訪問所有值 >>> dict1 = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'} >>> for key in dict1: print(key,dict1[key]) ob3 printer ob2 mouse ob1 computer
3.修改字典
>>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'} >>> dict['ob1']='book' >>> print(dict) {'ob3': 'printer', 'ob2': 'mouse', 'ob1': 'book'}
4.刪除字典
能刪單一的元素 >>> dict = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'} >>> del dict['ob1'] >>> print(dict) {'ob3': 'printer', 'ob2': 'mouse'} 刪除字典中所有元素 >>> dict1={'ob1':'computer','ob2':'mouse','ob1':'printer'} >>> dict1.clear() >>> print(dict1) {} 刪除整個字典,刪除后訪問字典會拋出異常。 >>> dict1 = {'ob1':'computer', 'ob2':'mouse', 'ob3':'printer'} >>> del dict1 >>> print(dict1) Traceback (most recent call last): File "", line 1, in print(dict1) NameError: name 'dict1' is not defined
5.更新字典
update()方法可以用來將一個字典的內容添加到另外一個字典中: >>> dict1 = {'ob1':'computer', 'ob2':'mouse'} >>> dict2={'ob3':'printer'} >>> dict1.update(dict2) >>> print(dict1) {'ob3': 'printer', 'ob2': 'mouse', 'ob1': 'computer'}
6.映射類型相關的函數
>>> dict(x=1, y=2) {'y': 2, 'x': 1} >>> dict8 = dict(x=1, y=2) >>> dict8 {'y': 2, 'x': 1} >>> dict9 = dict(**dict8) >>> dict9 {'y': 2, 'x': 1} dict9 = dict8.copy()
7.字典鍵的特性
字典值可以沒有限制地取任何python對象,既可以是標準的對象,也可以是用戶定義的,但鍵不行。 兩個重要的點需要記住: 1)不允許同一個鍵出現兩次。創建時如果同一個鍵被賦值兩次,后一個值會被記住 >>> dict1={'ob1':'computer','ob2':'mouse','ob1':'printer'} >>> print(dict1) {'ob2': 'mouse', 'ob1': 'printer'} 2)鍵必須不可變,所以可以用數,字符串或元組充當,用列表就不行 >>> dict1 = {['ob1']:'computer', 'ob2':'mouse', 'ob3':'printer'} Traceback (most recent call last): File "", line 1, in dict1 = {['ob1']:'computer', 'ob2':'mouse', 'ob3':'printer'} TypeError: unhashable type: 'list'
8.字典內置函數&方法
Python字典包含了以下內置函數: 1、cmp(dict1, dict2):比較兩個字典元素。(python3后不可用) 2、len(dict):計算字典元素個數,即鍵的總數。 3、str(dict):輸出字典可打印的字符串。 4、type(variable):返回輸入的變量類型,如果變量是字典就返回字典類型。 Python字典包含了以下內置方法: 1、radiansdict.clear():刪除字典內所有元素 2、radiansdict.copy():返回一個字典的淺復制 3、radiansdict.fromkeys():創建一個新字典,以序列seq中元素做字典的鍵,val為字典所有鍵對應的初始值 4、radiansdict.get(key, default=None):返回指定鍵的值,如果值不在字典中返回default值 5、radiansdict.has_key(key):如果鍵在字典dict里返回true,否則返回false 6、radiansdict.items():以列表返回可遍歷的(鍵, 值) 元組數組 7、radiansdict.keys():以列表返回一個字典所有的鍵 8、radiansdict.setdefault(key, default=None):和get()類似, 但如果鍵不已經存在于字典中,將會添加鍵并將值設為default 9、radiansdict.update(dict2):把字典dict2的鍵/值對更新到dict里 10、radiansdict.values():以列表返回字典中的所有值
以上這篇python字典的常用操作方法小結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
