python中的字典操作及字典函數(shù)
更新時間:2018年01月03日 09:33:47 作者:wiz_333
本篇文章給大家介紹了python中的字典,包括字典的操作,字典函數(shù)實現(xiàn)代碼,需要的朋友參考下吧
字典
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ù)制成一個新字典)
test2 = {2:'2'}
test2_copy = test2.copy()
print(test2_copy)
'''''
test2結(jié)果:
{2: '2'}
'''
#使用指定的key和value制作一個字典
list_test = ['a','b','c']
test3 = {}.fromkeys(list_test,'ojbk')
print(test3)
'''''
test3結(jié)果:
{'a': 'ojbk', 'b': 'ojbk', 'c': 'ojbk'}
'''
#將一個字典轉(zhuǎn)化為二級容器(中間容器)
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ù)(更新一個不存在的key時,可用于添加新數(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ù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
您可能感興趣的文章:
- Python字典常見操作實例小結(jié)【定義、添加、刪除、遍歷】
- Python 字典(Dictionary)操作詳解
- Python中字典創(chuàng)建、遍歷、添加等實用操作技巧合集
- Python中字典和JSON互轉(zhuǎn)操作實例
- Python3實現(xiàn)的字典遍歷操作詳解
- Python字典及字典基本操作方法詳解
- 整理Python最基本的操作字典的方法
- python 字典操作提取key,value的方法
- python字典的常用操作方法小結(jié)
- python基礎(chǔ)教程之字典操作詳解
- python基礎(chǔ)入門詳解(文件輸入/輸出 內(nèi)建類型 字典操作使用方法)
- Python基礎(chǔ)之字典常見操作經(jīng)典實例詳解
相關(guān)文章
詳解python statistics模塊及函數(shù)用法
本節(jié)介紹 Python 中的另一個常用模塊 —— statistics模塊,該模塊提供了用于計算數(shù)字?jǐn)?shù)據(jù)的數(shù)理統(tǒng)計量的函數(shù)。這篇文章重點給大家介紹python statistics 模塊的一些用法,感興趣的朋友跟隨小編一起看看吧2019-10-10
使用pickle存儲數(shù)據(jù)dump 和 load實例講解
今天小編就為大家分享一篇使用pickle存儲數(shù)據(jù)dump 和 load實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
python使用paramiko實現(xiàn)遠(yuǎn)程拷貝文件的方法
這篇文章主要介紹了python使用paramiko實現(xiàn)遠(yuǎn)程拷貝文件的方法,分析了paramiko庫的安裝以及遠(yuǎn)程下載文件的實現(xiàn)技巧,需要的朋友可以參考下2016-04-04
Python中函數(shù)參數(shù)調(diào)用方式分析
這篇文章主要介紹了Python中函數(shù)參數(shù)調(diào)用方式,結(jié)合實例形式分析了Python函數(shù)參數(shù)定義與使用的四種常見操作方法,需要的朋友可以參考下2018-08-08

