更新時(shí)間:2021-12-21 來源:黑馬程序員 瀏覽量:
字典是Python中比較常用的數(shù)據(jù)結(jié)構(gòu),字典中每個(gè)成員是以“鍵:值”對(duì)的形式存放具有映射關(guān)系的數(shù)據(jù)。
字典語法:
字典以大括號(hào)“{}”包圍的以“鍵:值”對(duì)方式聲明和存在的數(shù)據(jù)集合,“鍵:值”對(duì)之間用“英文逗號(hào)”隔開。
如下代碼創(chuàng)建了一個(gè)字典:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(scores_dict)
·字典的每個(gè)健值的展現(xiàn)方式是:key:value用冒號(hào)分割;
·鍵值之間為逗號(hào)分割;
·整個(gè)字典用大括號(hào){}將鍵值括起來;
·字典是無序的,它不能通過偏移來存取,只能通過鍵來存取;
·鍵必須是唯一;
·鍵必須是不可變的數(shù)據(jù)類型,比如,數(shù)字,字符串,元組等,列表等可變對(duì)象不能作為鍵;
·鍵值可以是任意類型的對(duì)象;
通過 key 訪問value,演示如下:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(scores_dict['語文']) # 通過鍵“語文”獲取對(duì)應(yīng)的值通過 key 添加 key-value 對(duì),演示如下:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
scores_dict['物理'] = 97 # 添加 ‘物理’: 97
print(scores_dict) # {'語文': 105, '數(shù)學(xué)': 140, '英語': 120, '物理': 97}·能刪單一的元素
通過 key 刪除 key-value 對(duì),演示如下:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
del scores_dict['數(shù)學(xué)'] # 刪除 ’語文‘: 105
print(scores_dict) # 輸出 {'語文': 105, '英語': 120}通過 key 修改 key-value 對(duì),演示如下:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
scores_dict['數(shù)學(xué)'] = 120 # 修改將“數(shù)學(xué)”修改為120
print(scores_dict) # 輸出 {'語文': 105, '數(shù)學(xué)': 120, '英語': 120}如果要判斷字典是否包含指定的 key,則可以使用 in 或 not in 運(yùn)算符。需要指出的是,對(duì)于 dict 而言,in 或 not in 運(yùn)算符都是基于 key 來判斷的。例如如下代碼:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
# 判斷scores_dict是否包含名為'語文'的key
print('語文' in scores_dict) # True
# 判斷scores_dict不包含'歷史'的key
print('歷史' not in scores_dict) # Trueclear() 用于清空字典中所有的 key-value 對(duì),對(duì)一個(gè)字典執(zhí)行 clear() 方法之后,該字典就會(huì)變成一個(gè)空字典。
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(scores_dict) # 輸出 {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
scores_dict.clear() # 刪除字典所有內(nèi)容
print(scores_dict) # 輸出{}get() 方法其實(shí)就是根據(jù) key 來獲取 value,它相當(dāng)于方括號(hào)語法的增強(qiáng)版,當(dāng)使用方括號(hào)語法訪問并不存在的 key 時(shí),字典會(huì)引發(fā) KeyError 錯(cuò)誤;但如果使用 get() 方法訪問不存在的 key,該方法會(huì)簡(jiǎn)單地返回 None,不會(huì)導(dǎo)致錯(cuò)誤。例如如下代碼:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(scores_dict.get('歷史')) # 輸出 None
print(scores_dict['歷史']) # 報(bào)錯(cuò) KeyError: '歷史'update() 方法可使用一個(gè)字典所包含的 key-value 對(duì)來更新己有的字典。在執(zhí)行 update() 方法時(shí),如果被更新的字典中己包含對(duì)應(yīng)的 key-value 對(duì),那么原 value 會(huì)被覆蓋;如果被更新的字典中不包含對(duì)應(yīng)的 key-value 對(duì),則該 key-value 對(duì)被添加進(jìn)去。例如如下代碼:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
scores_dict.update({'語文': 120, '數(shù)學(xué)': 110})
print(scores_dict) # 輸出{'語文': 120, '數(shù)學(xué)': 110, '英語': 120}以列表返回可遍歷的(鍵, 值) 元組數(shù)組
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(scores_dict.items()) # 輸出 dict_items([('語文', 105), ('數(shù)學(xué)', 140), ('英語', 120)])以列表返回一個(gè)字典所有的鍵
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(scores_dict.keys()) # 輸出 dict_keys(['語文', '數(shù)學(xué)', '英語'])
br/>
以列表返回字典中的所有值
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(scores_dict.values()) # 輸出 dict_values([105, 140, 120])pop() 方法用于獲取指定 key 對(duì)應(yīng)的 value,并刪除這個(gè) key-value 對(duì)。如下方法示范了 pop() 方法的用法:
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
scores_dict.pop('英語') # 刪除'英語'的鍵和值
print(scores_dict) # 輸出{'語文': 105, '數(shù)學(xué)': 140}
popitem() 方法用于彈出字典中最后一個(gè)key-value對(duì)
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(scores_dict.popitem()) # 輸出('英語', 120)setdefault() 方法也用于根據(jù) key 來獲取對(duì)應(yīng) value 的值。但該方法有一個(gè)額外的功能,即當(dāng)程序要獲取的 key 在字典中不存在時(shí),該方法會(huì)先為這個(gè)不存在的 key 設(shè)置一個(gè)默認(rèn)的 value,然后再返回該 key 對(duì)應(yīng)的值。
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
# 設(shè)置'語文'默認(rèn)值為100
scores_dict.setdefault('語文', 100)
print(scores_dict) # 輸出{'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
# 設(shè)置'歷史'默認(rèn)值為140
scores_dict.setdefault('歷史', 140)
print(scores_dict) # 輸出{'語文': 105, '數(shù)學(xué)': 140, '英語': 120, '歷史': 140}fromkeys() 方法使用給定的多個(gè)key創(chuàng)建字典,這些key對(duì)應(yīng)的value默認(rèn)都是None;也可以額外傳入一個(gè)參數(shù)作為默認(rèn)的value。該方法一般不會(huì)使用字典對(duì)象調(diào)用(沒什么意義),通常會(huì)使用 dict 類直接調(diào)用。例如如下代碼:
scores_dict = dict.fromkeys(['語文', '數(shù)學(xué)'])
print(scores_dict) # 輸出{'語文': None, '數(shù)學(xué)': None}
scores_dict = dict.fromkeys(('語文', '數(shù)學(xué)'))
print(scores_dict) # 輸出{'語文': None, '數(shù)學(xué)': None}
# 使用元組創(chuàng)建包含2個(gè)key的字典,指定默認(rèn)的value
scores_dict = dict.fromkeys(('語文', '數(shù)學(xué)'), 100)
print(scores_dict) # 輸出{'語文': 100, '數(shù)學(xué)': 100}計(jì)算字典元素個(gè)數(shù),即鍵的總數(shù)。
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(len(scores_dict)) # 輸出 3輸出字典可打印的字符串
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(str(scores_dict)) # 輸出{'語文': 105, '數(shù)學(xué)': 140, '英語': 120}返回輸入的變量類型,如果變量是字典就返回字典類型。
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
print(type(scores_dict)) # 輸出<class 'dict'>scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
for key in scores_dict:
print(key)
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
for value in scores_dict.values():
print(value)
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
for key in scores_dict:
print(key + ":" + str(scores_dict[key])) # 返回字符串
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
for i in scores_dict.items():
print(i) # 返回元組
scores_dict = {'語文': 105, '數(shù)學(xué)': 140, '英語': 120}
for key, value in scores_dict.items():
print(key + ':' + str(value))
加QQ:435946716獲取上面視頻的全套資料【視頻+筆記+源碼】
猜你喜歡: