最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python基礎(chǔ)知識(shí)之字典(Dict)

 更新時(shí)間:2023年02月19日 09:36:26   作者:51碼銀  
這篇文章主要介紹了python基礎(chǔ)知識(shí)之字典(Dict)的相關(guān)資料,需要的朋友可以參考下

 一、字典的基本操作

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))

python(13)--字典(Dict)_刪除元素

2.建立空字典

實(shí)例:

print("``````````````````````````````````````````````````````````")
flower={}
flower['rose']=13
flower['orchid']=16
print(flower)
print("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_數(shù)據(jù)結(jié)構(gòu)_04

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("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_刪除元素_07

如果有兩個(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("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_刪除元素_10

如上所示,字典中的元素對(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("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_數(shù)據(jù)結(jié)構(gòu)_13

 5.更改元素內(nèi)容

實(shí)例:

print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'紅茶':30,'綠茶':20,'茉莉花茶':40}
flower['rose']=13
print(flower)
print("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_刪除元素_15

6.刪除字典(特定元素)

刪除元素實(shí)例: 

print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
tea={'紅茶':30,'綠茶':20,'茉莉花茶':40}
del flower['rose']
print(flower)
print("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_數(shù)據(jù)結(jié)構(gòu)_18

 刪除字典實(shí)例:

print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
del flower
print(flower)
print("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_數(shù)據(jù)結(jié)構(gòu)_21

7. 字典的復(fù)制

print("``````````````````````````````````````````````````````````")
flower={'rose':10,'orchid':16,'carnation':8}
copyflower=flower.copy()
print(flower)
print(copyflower)
print("``````````````````````````````````````````````````````````")

python(13)--字典(Dict)_數(shù)據(jù)結(jié)構(gòu)_23

 二、遍歷字典

1.遍歷字典的key-value

flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers,price in flower.items():
print("花名:",flowers)
print("價(jià)格:",price)
print("\n")

python(13)--字典(Dict)_數(shù)據(jù)結(jié)構(gòu)_24

2.遍歷字典的鍵(key) 

flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower.keys():
print("花名:",flowers)
print("\n")

python(13)--字典(Dict)_python_26

沒(méi)有keys()函數(shù)也行:

flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower:
print("花名:",flowers)

python(13)--字典(Dict)_刪除元素_29

 3.遍歷字典的值(value)

flower={'rose':10,
'orchid':16,
'carnation':8}
for flowers in flower.values():
print("價(jià)格:",flowers)

python(13)--字典(Dict)_數(shù)據(jù)結(jié)構(gòu)_32

 4.字典里面放字典

實(shí)例:人物介紹

role={

'魯班':{
'技能':'土木建筑',
'職業(yè)':'工匠'
},
'鐘無(wú)艷':{
'技能':'出謀劃策',
'職業(yè)':'中國(guó)古代四大丑女之一'
},
'蔡文姬':{
'技能':'琴棋書(shū)畫',
'職業(yè)':'董祀之妻'
}

}
for a,b in role.items():
print("姓名:",a)
print("介紹:",b)

python(13)--字典(Dict)_python_35

三、簡(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實(shí)現(xiàn)將list拼接為一個(gè)字符串

    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實(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()示例詳解

    Python內(nèi)置函數(shù)input()示例詳解

    input()函數(shù)是Python中用于獲取用戶輸入的一個(gè)簡(jiǎn)單而強(qiáng)大的工具,它在創(chuàng)建需要用戶交互的程序時(shí)非常有用,這篇文章主要介紹了Python內(nèi)置函數(shù)input()詳解,需要的朋友可以參考下
    2024-04-04
  • Gradio中button組件的基本使用方式

    Gradio中button組件的基本使用方式

    Gradio中的button組件用于實(shí)現(xiàn)點(diǎn)擊事件,通過(guò)click事件綁定函數(shù)來(lái)處理值的變化,點(diǎn)擊時(shí),函數(shù)接收組件的值作為輸入,返回新的值或更新組件的屬性,示例代碼展示了如何在點(diǎn)擊按鈕時(shí)修改文本框的值并控制按鈕的可見(jiàn)性
    2024-11-11
  • Jupyter notebook之如何快速打開(kāi)ipynb文件

    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í)例

    這篇文章主要介紹了Python基礎(chǔ)教程之tcp socket編程詳解及簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • django url到views參數(shù)傳遞的實(shí)例

    django url到views參數(shù)傳遞的實(shí)例

    今天小編就為大家分享一篇django url到views參數(shù)傳遞的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • selenium+python 對(duì)輸入框的輸入處理方法

    selenium+python 對(duì)輸入框的輸入處理方法

    今天小編就為大家分享一篇selenium+python 對(duì)輸入框的輸入處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • 3個(gè)Python?SQLAlchemy數(shù)據(jù)庫(kù)操作功能詳解

    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ò)的操作

    這篇文章主要介紹了使用Pytorch訓(xùn)練two-head網(wǎng)絡(luò)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05

最新評(píng)論

尼木县| 灯塔市| 郑州市| 孟村| 新丰县| 清新县| 西乌珠穆沁旗| 若尔盖县| 枣强县| 清远市| 太原市| 阳朔县| 阿拉善右旗| 房产| 慈利县| 法库县| 巫山县| 惠州市| 余干县| 白山市| 苍梧县| 融水| 大城县| 九台市| 张家界市| 玉溪市| 景东| 民丰县| 土默特左旗| 莱州市| 寿光市| 广德县| 香河县| 隆德县| 高尔夫| 武隆县| 尉氏县| 宣化县| 会宁县| 纳雍县| 桦川县|