python基礎(chǔ)知識(shí)之字典(Dict)
一、字典的基本操作
1.定義字典
字典也是一個(gè)列表型的數(shù)據(jù)結(jié)構(gòu),字典的數(shù)據(jù)是用“{ }”裝的(列表:[ ],元組:( )),字典的元素是一一對(duì)應(yīng)的關(guān)系“key-value”。
格式:
Dictname={ key1:value1,...,key2:value2}
#value是任何的python的對(duì)象
#字典的元素?cái)?shù)量也是用len()函數(shù)
多說(shuō)無(wú)益,直接看例子比較清晰:
實(shí)例:
flower={'rose':10,'orchid':16,'carnation':8}
tea={'紅茶':30,'綠茶':20,'茉莉花茶':40}
print(flower)
print(tea)
print("字典flower的元素?cái)?shù)量是:",len(flower))
print("字典的數(shù)據(jù)類型:",type(tea))
2.建立空字典
實(shí)例:
print("``````````````````````````````````````````````````````````")
flower={}
flower['rose']=13
flower['orchid']=16
print(flower)
print("``````````````````````````````````````````````````````````")
3.列出字典元素的值
格式:
flower【'rose'】
#注意列出字典元素的值要用中括號(hào)哦“[ ]”
#上面語(yǔ)句表達(dá)的意思是字典 flower 的 rose(key)的對(duì)應(yīng) 10(value)值。
實(shí)例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'紅茶':30,'綠茶':20,'茉莉花茶':40}
print("一支玫瑰的價(jià)錢是:",flower['rose'])
print("紅茶一袋的價(jià)錢是:",tea['紅茶'])
print("``````````````````````````````````````````````````````````")
如果有兩個(gè)“rose”,兩個(gè)“紅茶”呢,元素對(duì)應(yīng)的值(value)是哪個(gè)呢?
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8,'rose':15}
tea={'紅茶':30,'綠茶':20,'茉莉花茶':40,'紅茶':13}
print("一支玫瑰的價(jià)錢是:",flower['rose'])
print("紅茶一袋的價(jià)錢是:",tea['紅茶'])
print("``````````````````````````````````````````````````````````")
如上所示,字典中的元素對(duì)應(yīng)值被后面的值占領(lǐng)了。
4.增加字典元素
實(shí)例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'紅茶':30,'綠茶':20,'茉莉花茶':40}
flower['tuilp']=13
print(flower)
print("``````````````````````````````````````````````````````````")
5.更改元素內(nèi)容
實(shí)例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'紅茶':30,'綠茶':20,'茉莉花茶':40}
flower['rose']=13
print(flower)
print("``````````````````````````````````````````````````````````")
6.刪除字典(特定元素)
刪除元素實(shí)例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'紅茶':30,'綠茶':20,'茉莉花茶':40}
del flower['rose']
print(flower)
print("``````````````````````````````````````````````````````````")
刪除字典實(shí)例:
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
del flower
print(flower)
print("``````````````````````````````````````````````````````````")
7. 字典的復(fù)制
print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
copyflower=flower.copy()
print(flower)
print(copyflower)
print("``````````````````````````````````````````````````````````")
二、遍歷字典
1.遍歷字典的key-value
flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers,price in flower.items():
print("花名:",flowers)
print("價(jià)格:",price)
print("\n")
2.遍歷字典的鍵(key)
flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower.keys():
print("花名:",flowers)
print("\n")
沒(méi)有keys()函數(shù)也行:
flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower:
print("花名:",flowers)
3.遍歷字典的值(value)
flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower.values():
print("價(jià)格:",flowers)
4.字典里面放字典
實(shí)例:人物介紹
role={
'魯班':{
'技能':'土木建筑',
'職業(yè)':'工匠'
},
'鐘無(wú)艷':{
'技能':'出謀劃策',
'職業(yè)':'中國(guó)古代四大丑女之一'
},
'蔡文姬':{
'技能':'琴棋書畫',
'職業(yè)':'董祀之妻'
}
}
for a,b in role.items():
print("姓名:",a)
print("介紹:",b)
三、簡(jiǎn)單介紹下函數(shù)
len():求元素個(gè)數(shù)
get():搜尋字典的key
格式:返回值=字典名.get('key')
pop():刪除元素
格式:返回值=字典名.pop('key')
到此這篇關(guān)于python基礎(chǔ)知識(shí)之字典(Dict)的文章就介紹到這了,更多相關(guān)python 字典 Dict內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python 字典(Dictionary)操作詳解
- python兩種遍歷字典(dict)的方法比較
- python通過(guò)字典dict判斷指定鍵值是否存在的方法
- python 將字符串轉(zhuǎn)換成字典dict的各種方式總結(jié)
- Python中字典(dict)合并的四種方法總結(jié)
- Python的“二維”字典 (two-dimension dictionary)定義與實(shí)現(xiàn)方法
- Python中字典(dict)和列表(list)的排序方法實(shí)例
- python 將字符串轉(zhuǎn)換成字典dict
- python 字典(dict)按鍵和值排序
- Python中實(shí)現(xiàn)兩個(gè)字典(dict)合并的方法
相關(guān)文章
解決pip安裝的第三方包在PyCharm無(wú)法導(dǎo)入的問(wèn)題
這篇文章主要介紹了關(guān)于pip安裝的第三方包在PyCharm無(wú)法導(dǎo)入的問(wèn)題,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
python正則表達(dá)式查找和替換內(nèi)容的實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python正則表達(dá)式查找和替換內(nèi)容的實(shí)例詳解內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)參考下。2021-10-10
OpenCV?Python身份證信息識(shí)別過(guò)程詳解
本篇文章使用OpenCV-Python和CnOcr來(lái)實(shí)現(xiàn)身份證信息識(shí)別的案例,本篇文章使用的Python版本為3.6,OpenCV-Python版本為3.4.1.15,如果是4.x版本的同學(xué),可能會(huì)有一些Api操作不同,下面跟隨小編看下OpenCV?Python身份證信息識(shí)別過(guò)程2022-04-04
Python二進(jìn)制數(shù)據(jù)結(jié)構(gòu)Struct的具體使用
在C/C++語(yǔ)言中,struct被稱為結(jié)構(gòu)體。而在Python中,struct是一個(gè)專門的庫(kù),用于處理字節(jié)串與原生Python數(shù)據(jù)結(jié)構(gòu)類型之間的轉(zhuǎn)換。本文就詳細(xì)介紹struct的使用方式2021-06-06
pandas實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式
這篇文章主要介紹了pandas實(shí)現(xiàn)導(dǎo)出數(shù)據(jù)的四種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python實(shí)用小技巧之判斷輸入是否為漢字/英文/數(shù)字
這篇文章主要給大家介紹了關(guān)于Python實(shí)用小技巧之判斷輸入是否為漢字/英文/數(shù)字的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-06-06
python opencv 讀取圖片 返回圖片某像素點(diǎn)的b,g,r值的實(shí)現(xiàn)方法
今天小編就為大家分享一篇python opencv 讀取圖片 返回圖片某像素點(diǎn)的b,g,r值的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07

