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ó)古代四大丑女之一'
},
'蔡文姬':{
'技能':'琴棋書(shū)畫',
'職業(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)文章
python實(shí)現(xiàn)將list拼接為一個(gè)字符串
這篇文章主要介紹了python實(shí)現(xiàn)將list拼接為一個(gè)字符串方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
基于Python實(shí)現(xiàn)將列表數(shù)據(jù)生成折線圖
這篇文章主要介紹了如何利用Python中的pandas庫(kù)和matplotlib庫(kù),實(shí)現(xiàn)將列表數(shù)據(jù)生成折線圖,文中的示例代碼簡(jiǎn)潔易懂,需要的可以參考一下2022-03-03
Python內(nèi)置函數(shù)input()示例詳解
input()函數(shù)是Python中用于獲取用戶輸入的一個(gè)簡(jiǎn)單而強(qiáng)大的工具,它在創(chuàng)建需要用戶交互的程序時(shí)非常有用,這篇文章主要介紹了Python內(nèi)置函數(shù)input()詳解,需要的朋友可以參考下2024-04-04
Jupyter notebook之如何快速打開(kāi)ipynb文件
這篇文章主要介紹了Jupyter notebook之如何快速打開(kāi)ipynb文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
Python基礎(chǔ)教程之tcp socket編程詳解及簡(jiǎn)單實(shí)例
這篇文章主要介紹了Python基礎(chǔ)教程之tcp socket編程詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
django url到views參數(shù)傳遞的實(shí)例
今天小編就為大家分享一篇django url到views參數(shù)傳遞的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
selenium+python 對(duì)輸入框的輸入處理方法
今天小編就為大家分享一篇selenium+python 對(duì)輸入框的輸入處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
3個(gè)Python?SQLAlchemy數(shù)據(jù)庫(kù)操作功能詳解
Python?SQLAlchemy?是一個(gè)強(qiáng)大且多功能的?Python?SQL?工具包和對(duì)象關(guān)系映射?(ORM)?系統(tǒng),提供了一整套眾所周知的企業(yè)級(jí)持久性模式,本文為大家整理了它必須了解的3個(gè)數(shù)據(jù)庫(kù)操作功能,希望對(duì)大家有所幫助2023-09-09
使用Pytorch訓(xùn)練two-head網(wǎng)絡(luò)的操作
這篇文章主要介紹了使用Pytorch訓(xùn)練two-head網(wǎng)絡(luò)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05

