Python字典及字典基本操作方法詳解
本文實例講述了Python字典及字典基本操作方法。分享給大家供大家參考,具體如下:
字典是一種通過名字或者關(guān)鍵字引用的得數(shù)據(jù)結(jié)構(gòu),其鍵可以是數(shù)字、字符串、元組,這種結(jié)構(gòu)類型也稱之為映射。字典類型是Python中唯一內(nèi)建的映射類型,基本的操作包括如下:
(1)len():返回字典中鍵—值對的數(shù)量;
(2)d[k]:返回關(guān)鍵字對于的值;
(3)d[k]=v:將值關(guān)聯(lián)到鍵值k上;
(4)del d[k]:刪除鍵值為k的項;
(5)key in d:鍵值key是否在d中,是返回True,否則返回False。
一、字典的創(chuàng)建
1.1 直接創(chuàng)建字典
d={'one':1,'two':2,'three':3}
print d
print d['two']
print d['three']
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
2
3
>>>
1.2 通過dict創(chuàng)建字典
# _*_ coding:utf-8 _*_
items=[('one',1),('two',2),('three',3),('four',4)]
print u'items中的內(nèi)容:'
print items
print u'利用dict創(chuàng)建字典,輸出字典內(nèi)容:'
d=dict(items)
print d
print u'查詢字典中的內(nèi)容:'
print d['one']
print d['three']
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
items中的內(nèi)容:
[('one', 1), ('two', 2), ('three', 3), ('four', 4)]
利用dict創(chuàng)建字典,輸出字典內(nèi)容:
{'four': 4, 'three': 3, 'two': 2, 'one': 1}
查詢字典中的內(nèi)容:
1
3
>>>
或者通過關(guān)鍵字創(chuàng)建字典
# _*_ coding:utf-8 _*_ d=dict(one=1,two=2,three=3) print u'輸出字典內(nèi)容:' print d print u'查詢字典中的內(nèi)容:' print d['one'] print d['three']
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
輸出字典內(nèi)容:
{'three': 3, 'two': 2, 'one': 1}
查詢字典中的內(nèi)容:
1
3
>>>
二、字典的格式化字符串
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3,'four':4}
print d
print "three is %(three)s." %d
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'four': 4, 'three': 3, 'two': 2, 'one': 1}
three is 3.
>>>
三、字典方法
3.1 clear函數(shù):清除字典中的所有項
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3,'four':4}
print d
d.clear()
print d
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'four': 4, 'three': 3, 'two': 2, 'one': 1}
{}
>>>
請看下面兩個例子
3.1.1
# _*_ coding:utf-8 _*_
d={}
dd=d
d['one']=1
d['two']=2
print dd
d={}
print d
print dd
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'two': 2, 'one': 1}
{}
{'two': 2, 'one': 1}
>>>
3.1.2
# _*_ coding:utf-8 _*_
d={}
dd=d
d['one']=1
d['two']=2
print dd
d.clear()
print d
print dd
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'two': 2, 'one': 1}
{}
{}
>>>
3.1.2與3.1.1唯一不同的是在對字典d的清空處理上,3.1.1將d關(guān)聯(lián)到一個新的空字典上,這種方式對字典dd是沒有影響的,所以在字典d被置空后,字典dd里面的值仍舊沒有變化。但是在3.1.2中clear方法清空字典d中的內(nèi)容,clear是一個原地操作的方法,使得d中的內(nèi)容全部被置空,這樣dd所指向的空間也被置空。
3.2 copy函數(shù):返回一個具有相同鍵值的新字典
# _*_ coding:utf-8 _*_
x={'one':1,'two':2,'three':3,'test':['a','b','c']}
print u'初始X字典:'
print x
print u'X復(fù)制到Y(jié):'
y=x.copy()
print u'Y字典:'
print y
y['three']=33
print u'修改Y中的值,觀察輸出:'
print y
print x
print u'刪除Y中的值,觀察輸出'
y['test'].remove('c')
print y
print x
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
初始X字典:
{'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}
X復(fù)制到Y(jié):
Y字典:
{'test': ['a', 'b', 'c'], 'one': 1, 'three': 3, 'two': 2}
修改Y中的值,觀察輸出:
{'test': ['a', 'b', 'c'], 'one': 1, 'three': 33, 'two': 2}
{'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}
刪除Y中的值,觀察輸出
{'test': ['a', 'b'], 'one': 1, 'three': 33, 'two': 2}
{'test': ['a', 'b'], 'three': 3, 'two': 2, 'one': 1}
>>>
注:在復(fù)制的副本中對值進行替換后,對原來的字典不產(chǎn)生影響,但是如果修改了副本,原始的字典也會被修改。deepcopy函數(shù)使用深復(fù)制,復(fù)制其包含所有的值,這個方法可以解決由于副本修改而使原始字典也變化的問題。
# _*_ coding:utf-8 _*_
from copy import deepcopy
x={}
x['test']=['a','b','c','d']
y=x.copy()
z=deepcopy(x)
print u'輸出:'
print y
print z
print u'修改后輸出:'
x['test'].append('e')
print y
print z
運算輸出:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
輸出:
{'test': ['a', 'b', 'c', 'd']}
{'test': ['a', 'b', 'c', 'd']}
修改后輸出:
{'test': ['a', 'b', 'c', 'd', 'e']}
{'test': ['a', 'b', 'c', 'd']}
>>>
3.3 fromkeys函數(shù):使用給定的鍵建立新的字典,鍵默認對應(yīng)的值為None
# _*_ coding:utf-8 _*_ d=dict.fromkeys(['one','two','three']) print d
運算輸出:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': None, 'two': None, 'one': None}
>>>
或者指定默認的對應(yīng)值
# _*_ coding:utf-8 _*_ d=dict.fromkeys(['one','two','three'],'unknow') print d
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 'unknow', 'two': 'unknow', 'one': 'unknow'}
>>>
3.4 get函數(shù):訪問字典成員
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
print d.get('one')
print d.get('four')
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
1
None
>>>
注:get函數(shù)可以訪問字典中不存在的鍵,當(dāng)該鍵不存在是返回None
3.5 has_key函數(shù):檢查字典中是否含有給出的鍵
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
print d.has_key('one')
print d.has_key('four')
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
True
False
>>>
3.6 items和iteritems函數(shù):items將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
list=d.items()
for key,value in list:
print key,':',value
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
three : 3
two : 2
one : 1
>>>
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
it=d.iteritems()
for k,v in it:
print "d[%s]="%k,v
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
d[three]= 3
d[two]= 2
d[one]= 1
>>>
3.7 keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
print u'keys方法:'
list=d.keys()
print list
print u'\niterkeys方法:'
it=d.iterkeys()
for x in it:
print x
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
keys方法:
['three', 'two', 'one']
iterkeys方法:
three
two
one
>>>
3.8 pop函數(shù):刪除字典中對應(yīng)的鍵
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
d.pop('one')
print d
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
{'three': 3, 'two': 2}
>>>
3.9 popitem函數(shù):移出字典中的項
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
d.popitem()
print d
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 1}
{'two': 2, 'one': 1}
>>>
3.10 setdefault函數(shù):類似于get方法,獲取與給定鍵相關(guān)聯(lián)的值,也可以在字典中不包含給定鍵的情況下設(shè)定相應(yīng)的鍵值
# _*_ coding:utf-8 _*_
d={'one':1,'two':2,'three':3}
print d
print d.setdefault('one',1)
print d.setdefault('four',4)
print d
運算結(jié)果:
{'three': 3, 'two': 2, 'one': 1}
1
4
{'four': 4, 'three': 3, 'two': 2, 'one': 1}
>>>
3.11 update函數(shù):用一個字典更新另外一個字典
# _*_ coding:utf-8 _*_
d={
'one':123,
'two':2,
'three':3
}
print d
x={'one':1}
d.update(x)
print d
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{'three': 3, 'two': 2, 'one': 123}
{'three': 3, 'two': 2, 'one': 1}
>>>
3.12 values和itervalues函數(shù):values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復(fù)的元素
# _*_ coding:utf-8 _*_
d={
'one':123,
'two':2,
'three':3,
'test':2
}
print d.values()
運算結(jié)果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py======= [2, 3, 2, 123] >>>
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字典操作技巧匯總》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python dataprep庫簡化加速數(shù)據(jù)科學(xué)操作
這篇文章主要為大家介紹了python dataprep庫簡化加速數(shù)據(jù)科學(xué)操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
Python中TypeError: unhashable type: ‘list‘錯誤的解
在Python編程的領(lǐng)域中,數(shù)據(jù)類型的正確使用是確保程序正常運行的關(guān)鍵要素之一,然而,開發(fā)者們常常會遇到一些由于數(shù)據(jù)類型使用不當(dāng)而引發(fā)的報錯,其中TypeError: unhashable type: 'list’就是一個比較典型的錯誤,那么,讓我們深入探究這個報錯問題,為大家提供全面的解決方案2024-10-10
Python實現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運算示例
這篇文章主要介紹了Python實現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運算,結(jié)合實例形式分析了Python針對矩陣進行轉(zhuǎn)置與相乘運算的相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2019-03-03
使用Python+selenium實現(xiàn)第一個自動化測試腳本
這篇文章主要介紹了使用Python+selenium實現(xiàn)第一個自動化測試腳本,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03

