CAD小姐姐學(xué)Python,學(xué)習(xí)筆記第四篇,字典的知識(shí)
本文是《CAD小姐姐從零學(xué)Python》系列中的第四篇:
CAD小姐姐學(xué)Python,學(xué)習(xí)筆記第三篇,列表的使用
小白學(xué)Python,分享下我自己的學(xué)習(xí)筆記【二】
小白學(xué)Python,分享下我自己的學(xué)習(xí)筆記
大家好!新年伊始來給螞蟻老師投的第一稿,依舊是學(xué)習(xí)筆記。俗話說的好,“師傅領(lǐng)進(jìn)門,修行看個(gè)人”,學(xué)習(xí)還是要腳踏實(shí)地,一步步循序漸進(jìn),打好基礎(chǔ)才能在后面寫代碼的時(shí)候更輕松,所以每個(gè)基礎(chǔ)知識(shí)點(diǎn)都在舉一反三,視頻學(xué)習(xí)--->思考--->整理筆記,今天分享的是Python中的字典(dict)詳細(xì)使用,希望我的學(xué)習(xí)筆記能給大家的學(xué)習(xí)帶來幫助,同時(shí)也祝大家新的一年新氣象,早日成為程序猿。
字典
Python內(nèi)置的數(shù)據(jù)結(jié)構(gòu)之一,是一種映射結(jié)構(gòu)的數(shù)據(jù)結(jié)構(gòu)類型,與列表一樣是一個(gè)可變序列。以鍵值對(duì)的方式存儲(chǔ)數(shù)據(jù),字典是一個(gè)無序的序列。字典的key要求不可變,可用字符串、數(shù)字、元組,不能是可變的序列,字典value可以是python中的任何數(shù)據(jù)類型。
scores={'小明’:100,‘小紅’:98,‘小剛’:59}
字典的實(shí)現(xiàn)原理:
字典的實(shí)現(xiàn)原理與查字典類似,查字典是先根據(jù)部首或拼音查找相應(yīng)的頁碼,Python中的字典是根據(jù)key查找value所在的位置
字典的特點(diǎn)
字典中的所有元素都是一個(gè)key-value對(duì),key不允許重復(fù),value可以重復(fù)
字典中的元素是無序的
字典中的key必須是不可變的對(duì)象
字典也可以根據(jù)需要?jiǎng)討B(tài)的伸縮
字典會(huì)浪費(fèi)比較大的內(nèi)存,是一種使用空間換時(shí)間的數(shù)據(jù)結(jié)構(gòu)
字典的創(chuàng)建
最常用的方式:{}
scores={'張三':100,'李四':98,'王五':59}
使用內(nèi)置函數(shù)dict()
dict(name='jack',age=20)
例:
scores={'張三':100,'李四':98,'王五':59}
print(scores)
print(type(scores))????
{'張三': 100, '李四': 98, '王五': 59}
dict(name='Baby',age=18)???
{'name': 'Baby', 'age': 18}
d={}
print(d)??????#空字典
{}
字典中元素的獲取
1.[] 舉例:scores['張三']
2.get()方法 ?舉例:scores.get('張三')
兩種方法的區(qū)別:
如果字典中不存在指定的key,scores[]是報(bào)錯(cuò)KeyError,
scores.get()則是返回None,還可以通過參數(shù)設(shè)置默認(rèn)值,當(dāng)Key不存在時(shí)返回
例:
scores={'張三':100,'李四':98,'王五':59}
print(scores['張三'])?????????#第一種方法
print(scores.get('張三'))?????#第二種方法
100
100
scores={'張三':100,'李四':98,'王五':59}
print(scores['李明'])?????????#報(bào)錯(cuò)?KeyError
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_11836/1268120967.py in
1 scores={'張三':100,'李四':98,'王五':59}
----> 2 print(scores['李明']) #報(bào)錯(cuò) KeyError
KeyError: '李明'
scores={'張三':100,'李四':98,'王五':59}
print(scores.get('李明'))
print(scores.get('李明',96))??#?96是在查找‘李明’所對(duì)應(yīng)的值Value不存在時(shí),提供的一個(gè)默認(rèn)值
None
96
字典的常用操作
1.key的判斷
in-->指定的key在字典中存在返回True --> '張三' in scores
not in -->指定的key在字典中不存在返回True --> 'Marry' not in scores
2.字典元素的刪除
del scores['張三'] ?刪除指定的key-value對(duì)
scores.clear() ? ?清空字典的元素
3.字典元素的新增、修改
scores['Jack']=90
例:
scores={'張三':100,'李四':98,'王五':59}
print('張三'?in?scores)?????????????#指定的key在字典中存在
print('李四'?not?in?scores)????????#指定的key在字典中農(nóng)不存在?
del?scores['張三']?????????????????#刪除指定的鍵值對(duì)
print(scores)
scores.clear()?????????????????????#清空字典的元素
print(scores)
True
False
{'李四': 98, '王五': 59}
{}
scores={'張三':100,'李四':98,'王五':59}
scores['陳六']=96???????????????#新增元素
print(scores)
scores['陳六']=100??????????????#修改元素
print(scores)
{'張三': 100, '李四': 98, '王五': 59, '陳六': 96}
{'張三': 100, '李四': 98, '王五': 59, '陳六': 100}
4.獲取字典視圖的三個(gè)方法
1.keys():獲取字典中所有的key
2.values():獲取字典中所有的values
3.items() :獲取字典中所有key:value對(duì)
例1:keys()
scores={'張三':100,'李四':98,'王五':59}
a=scores.keys()
print(a)???????????#獲取字典中所有的key
print(type(a))
print(list(a))?????#將所有的key組成的視圖轉(zhuǎn)化成列表
dict_keys(['張三', '李四', '王五'])
['張三', '李四', '王五']
例2:values()
scores={'張三':100,'李四':98,'王五':59}
a=scores.values()???????????#獲取字典中所有的values
print(a)
print(type(a))
print(list(a))??????????????#將所有的value組成的視圖轉(zhuǎn)化成列表
dict_values([100, 98, 59])
[100, 98, 59]
例3:items()
scores={'張三':100,'李四':98,'王五':59}
a=scores.items()???????????#獲取字典中所有的key-value對(duì)
print(a)
print(type(a))
print(list(a))?????????????#將所有的key-value對(duì)組成的視圖轉(zhuǎn)化成列表?
dict_items([('張三', 100), ('李四', 98), ('王五', 59)])
[('張三', 100), ('李四', 98), ('王五', 59)]
5.字典元素的遍歷
for item in scores:
print(item)
例:
scores={'張三':100,'李四':98,'王五':59}
for?item?in?scores:
????print(item)????????????????????#遍歷字典里的key
????print(item,scores[item])???????#遍歷字典里key和value
????print(item,scores.get(item))???#遍歷字典里key和value
張三
張三 100
張三 100
李四
李四 98
李四 98
王五
王五 59
王五 59
6.字典生成式
內(nèi)置函數(shù)zip()
用于將可迭代的對(duì)象作為參數(shù),將對(duì)象中對(duì)應(yīng)的元素打包成一個(gè)元組,然后返回由這些元組組成的列表
items=['Fruits','Bools','Others']
prices=[96,78,85]
lst=zip(items,prices)
print(list(lst))
字典生成式
{item.upper():price ?for item,price in zip(items,prices)}
例:
items=['Baby','Tom','Linda']
prices=[98,95,99]
a={item:price??for?item,price?in?zip(items,prices)}
print(a)
a={item.upper():price??for?item,price?in?zip(items,prices)}???#upper,全部大寫
print(a)
{'Baby': 98, 'Tom': 95, 'Linda': 99}
{'BABY': 98, 'TOM': 95, 'LINDA': 99}
items=['Baby','Tom','Linda']
prices=[98,95,99,100,80]
a={item.upper():price??for?item,price?in?zip(items,prices)}??
print(a)????????????#當(dāng)key?和value數(shù)量不同的時(shí)候,會(huì)以元素少的那個(gè)為基準(zhǔn)生成鍵值對(duì)
{'BABY': 98, 'TOM': 95, 'LINDA': 99}
最后,推薦螞蟻老師的《零基礎(chǔ)學(xué)Python》視頻課程系列,非常推薦
