python中的字典操作及字典函數(shù)
字典
dict_fruit = {'apple':'蘋果','banana':'香蕉','cherry':'櫻桃','avocado':'牛油果','watermelon':'西瓜'}
字典的操作
#字典的遍歷方式 #默認(rèn)遍歷(遍歷key) for value in dict_fruit: print(value) ''''' 遍歷出的值: watermelon apple cherry avocado banana ''' #使用key遍歷(與默認(rèn)遍歷一樣) for key in dict_fruit.keys(): print(key) ''''' 遍歷出的值: watermelon apple cherry avocado banana ''' #使用value遍歷 for value in dict_fruit.values(): print(value) ''''' 遍歷出的值: 蘋果 牛油果 香蕉 西瓜 櫻桃 ''' #使用key,value遍歷 for key,value in dict_fruit.items(): print(key+'--->'+value) ''''' 遍歷出的值: avocado--->牛油果 apple--->蘋果 banana--->香蕉 cherry--->櫻桃 watermelon--->西瓜 ''' #創(chuàng)建字典 #使用dict() res = dict(brand = '品牌',size='尺碼',color='顏色') print(res,type(res)) ''''' res結(jié)果: {'size': '尺碼', 'brand': '品牌', 'color': '顏色'} <class 'dict'> ''' #使用zip()和dict() keys = ['1','2','3','4','5'] values = [1,2,3,4,5] res = dict(zip(keys,values)) print(res,type(res)) ''''' res結(jié)果: {'3': 3, '4': 4, '1': 1, '2': 2, '5': 5} <class 'dict'> ''' #字典的推導(dǎo)式 res = {k+'的中文是'+v for k,v in dict_fruit.items()} print(res) ''''' res結(jié)果: {'watermelon的中文是西瓜', 'avocado的中文是牛油果', 'banana的中文是香蕉', 'cherry的中文是櫻桃', 'apple的中文是蘋果'} '''
字典的函數(shù)
#清空字典 test1 = {1:'1'} test1.clear() print(test1) ''''' test1結(jié)果: {} ''' #復(fù)制字典(復(fù)制成一個(gè)新字典) test2 = {2:'2'} test2_copy = test2.copy() print(test2_copy) ''''' test2結(jié)果: {2: '2'} ''' #使用指定的key和value制作一個(gè)字典 list_test = ['a','b','c'] test3 = {}.fromkeys(list_test,'ojbk') print(test3) ''''' test3結(jié)果: {'a': 'ojbk', 'b': 'ojbk', 'c': 'ojbk'} ''' #將一個(gè)字典轉(zhuǎn)化為二級(jí)容器(中間容器) res = dict_fruit.items() print(res,type(res)) ''''' res結(jié)果: dict_items([('avocado', '牛油果'), ('apple', '蘋果'), ('banana', '香蕉'), ('watermelon', '西瓜'), ('cherry', '櫻桃')]) <class 'dict_items'> ''' #將字典的key組成新的容器 res = dict_fruit.keys() print(res,type(res)) ''''' res結(jié)果: dict_keys(['watermelon', 'cherry', 'avocado', 'apple', 'banana']) <class 'dict_keys'> ''' #將字典的value組成新的容器 res = dict_fruit.values() print(res,type(res)) ''''' res結(jié)果: dict_values(['牛油果', '香蕉', '櫻桃', '蘋果', '西瓜']) <class 'dict_values'> ''' #根據(jù)key刪除字典中的數(shù)據(jù) test4 = {1:'1',2:'2',3:'3'} test4.pop(2) print(test4) ''''' test4結(jié)果: {1: '1', 3: '3'} ''' #依次彈出(刪除)字典中的數(shù)據(jù) test5 = {1:'1',2:'2',3:'3',4:'4',5:'5'} test5.popitem() print(test5) test5.popitem() print(test5) test5.popitem() print(test5) ''''' test5依次結(jié)果: {2: '2', 3: '3', 4: '4', 5: '5'} {3: '3', 4: '4', 5: '5'} {4: '4', 5: '5'} ''' #更新dict中的數(shù)據(jù)(更新一個(gè)不存在的key時(shí),可用于添加新數(shù)據(jù)) test6 = {'super':'Eric','ssuper':'Cbabe','sssuper':'Gogo','supreme':'wiz333'} #更新數(shù)據(jù) test6.update(super='Eric-LPL') print(test6) #添加數(shù)據(jù) test6.update(niceboy='Bigmao') print(test6) ''''' test6依次結(jié)果: {'ssuper': 'Cbabe', 'supreme': 'wiz333', 'sssuper': 'Gogo', 'super': 'Eric-LPL'} {'ssuper': 'Cbabe', 'supreme': 'wiz333', 'niceboy': 'Bigmao', 'sssuper': 'Gogo', 'super': 'Eric-LPL'} ''' #獲取dict中的數(shù)據(jù)(使用key獲取) test7 = {1:'1',2:'2',3:'3',4:'4',5:'5'} res = test7.get(1) print(res,type(res)) ''''' test7結(jié)果: 1 <class 'str'> ''' #給dict添加數(shù)據(jù)(setdefault,不能用于更新數(shù)據(jù)) test8 = {1:'1',2:'2',3:'3',4:'4',5:'5'} test8.setdefault(6,'6') print(test8) ''''' test8結(jié)果: {1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6'} '''
總結(jié)
以上所述是小編給大家介紹的python中的字典操作及字典函數(shù),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Python字典常見操作實(shí)例小結(jié)【定義、添加、刪除、遍歷】
- Python 字典(Dictionary)操作詳解
- Python中字典創(chuàng)建、遍歷、添加等實(shí)用操作技巧合集
- Python中字典和JSON互轉(zhuǎn)操作實(shí)例
- Python3實(shí)現(xiàn)的字典遍歷操作詳解
- Python字典及字典基本操作方法詳解
- 整理Python最基本的操作字典的方法
- python 字典操作提取key,value的方法
- python字典的常用操作方法小結(jié)
- python基礎(chǔ)教程之字典操作詳解
- python基礎(chǔ)入門詳解(文件輸入/輸出 內(nèi)建類型 字典操作使用方法)
- Python基礎(chǔ)之字典常見操作經(jīng)典實(shí)例詳解
相關(guān)文章
Django為窗體加上防機(jī)器人的驗(yàn)證碼功能過(guò)程解析
這篇文章主要介紹了Django為窗體加上防機(jī)器人的驗(yàn)證碼功能過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08詳解python statistics模塊及函數(shù)用法
本節(jié)介紹 Python 中的另一個(gè)常用模塊 —— statistics模塊,該模塊提供了用于計(jì)算數(shù)字?jǐn)?shù)據(jù)的數(shù)理統(tǒng)計(jì)量的函數(shù)。這篇文章重點(diǎn)給大家介紹python statistics 模塊的一些用法,感興趣的朋友跟隨小編一起看看吧2019-10-10Python?虛擬環(huán)境的價(jià)值和常用命令詳解
在實(shí)際項(xiàng)目開發(fā)中,我們通常會(huì)根據(jù)自己的需求去下載各種相應(yīng)的框架庫(kù),如Scrapy、Beautiful?Soup等,但是可能每個(gè)項(xiàng)目使用的框架庫(kù)并不一樣,或使用框架的版本不一樣,今天給大家分享下Python?虛擬環(huán)境的價(jià)值和常用命令,感興趣的朋友一起看看吧2022-05-05使用pickle存儲(chǔ)數(shù)據(jù)dump 和 load實(shí)例講解
今天小編就為大家分享一篇使用pickle存儲(chǔ)數(shù)據(jù)dump 和 load實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12python使用paramiko實(shí)現(xiàn)遠(yuǎn)程拷貝文件的方法
這篇文章主要介紹了python使用paramiko實(shí)現(xiàn)遠(yuǎn)程拷貝文件的方法,分析了paramiko庫(kù)的安裝以及遠(yuǎn)程下載文件的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04Python中函數(shù)參數(shù)調(diào)用方式分析
這篇文章主要介紹了Python中函數(shù)參數(shù)調(diào)用方式,結(jié)合實(shí)例形式分析了Python函數(shù)參數(shù)定義與使用的四種常見操作方法,需要的朋友可以參考下2018-08-08