国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python字典及字典基本操作方法詳解

2020-01-04 16:01:28
字體:
來源:轉載
供稿:網友

本文實例講述了Python字典及字典基本操作方法。分享給大家供大家參考,具體如下:

字典是一種通過名字或者關鍵字引用的得數據結構,其鍵可以是數字、字符串、元組,這種結構類型也稱之為映射。字典類型是Python中唯一內建的映射類型,基本的操作包括如下:

(1)len():返回字典中鍵—值對的數量;
(2)d[k]:返回關鍵字對于的值;
(3)d[k]=v:將值關聯到鍵值k上;
(4)del d[k]:刪除鍵值為k的項;
(5)key in d:鍵值key是否在d中,是返回True,否則返回False。

一、字典的創建

1.1 直接創建字典

d={'one':1,'two':2,'three':3}print dprint d['two']print d['three']

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 1}23>>>

1.2 通過dict創建字典

# _*_ coding:utf-8 _*_items=[('one',1),('two',2),('three',3),('four',4)]print u'items中的內容:'print itemsprint u'利用dict創建字典,輸出字典內容:'d=dict(items)print dprint u'查詢字典中的內容:'print d['one']print d['three']

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py=======items中的內容:[('one', 1), ('two', 2), ('three', 3), ('four', 4)]利用dict創建字典,輸出字典內容:{'four': 4, 'three': 3, 'two': 2, 'one': 1}查詢字典中的內容:13>>>

或者通過關鍵字創建字典

# _*_ coding:utf-8 _*_d=dict(one=1,two=2,three=3)print u'輸出字典內容:'print dprint u'查詢字典中的內容:'print d['one']print d['three']

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py=======輸出字典內容:{'three': 3, 'two': 2, 'one': 1}查詢字典中的內容:13>>>

二、字典的格式化字符串

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3,'four':4}print dprint "three is %(three)s." %d

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'four': 4, 'three': 3, 'two': 2, 'one': 1}three is 3.>>>

三、字典方法

3.1 clear函數:清除字典中的所有項

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3,'four':4}print dd.clear()print d

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'four': 4, 'three': 3, 'two': 2, 'one': 1}{}>>>

請看下面兩個例子

3.1.1

# _*_ coding:utf-8 _*_d={}dd=dd['one']=1d['two']=2print ddd={}print dprint dd

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'two': 2, 'one': 1}{}{'two': 2, 'one': 1}>>>

3.1.2

# _*_ coding:utf-8 _*_d={}dd=dd['one']=1d['two']=2print ddd.clear()print dprint dd

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'two': 2, 'one': 1}{}{}>>>

3.1.2與3.1.1唯一不同的是在對字典d的清空處理上,3.1.1將d關聯到一個新的空字典上,這種方式對字典dd是沒有影響的,所以在字典d被置空后,字典dd里面的值仍舊沒有變化。但是在3.1.2中clear方法清空字典d中的內容clear是一個原地操作的方法,使得d中的內容全部被置空,這樣dd所指向的空間也被置空

3.2 copy函數:返回一個具有相同鍵值的新字典

# _*_ coding:utf-8 _*_x={'one':1,'two':2,'three':3,'test':['a','b','c']}print u'初始X字典:'print xprint u'X復制到Y:'y=x.copy()print u'Y字典:'print yy['three']=33print u'修改Y中的值,觀察輸出:'print yprint xprint u'刪除Y中的值,觀察輸出'y['test'].remove('c')print yprint x

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py=======初始X字典:{'test': ['a', 'b', 'c'], 'three': 3, 'two': 2, 'one': 1}X復制到Y: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}>>>

注:在復制的副本中對值進行替換后,對原來的字典不產生影響,但是如果修改了副本,原始的字典也會被修改deepcopy函數使用深復制,復制其包含所有的值,這個方法可以解決由于副本修改而使原始字典也變化的問題。

# _*_ coding:utf-8 _*_from copy import deepcopyx={}x['test']=['a','b','c','d']y=x.copy()z=deepcopy(x)print u'輸出:'print yprint zprint u'修改后輸出:'x['test'].append('e')print yprint 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函數:使用給定的鍵建立新的字典,鍵默認對應的值為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}>>>

或者指定默認的對應值

# _*_ coding:utf-8 _*_d=dict.fromkeys(['one','two','three'],'unknow')print d

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 'unknow', 'two': 'unknow', 'one': 'unknow'}>>>

3.4 get函數:訪問字典成員

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3}print dprint d.get('one')print d.get('four')

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 1}1None>>>

注:get函數可以訪問字典中不存在的鍵,當該鍵不存在是返回None

3.5 has_key函數:檢查字典中是否含有給出的鍵

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3}print dprint d.has_key('one')print d.has_key('four')

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 1}TrueFalse>>>

3.6 items和iteritems函數:items將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3}print dlist=d.items()for key,value in list:  print key,':',value

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 1}three : 3two : 2one : 1>>>
# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3}print dit=d.iteritems()for k,v in it:  print "d[%s]="%k,v

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 1}d[three]= 3d[two]= 2d[one]= 1>>>

3.7 keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3}print dprint u'keys方法:'list=d.keys()print listprint u'/niterkeys方法:'it=d.iterkeys()for x in it:  print x

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 1}keys方法:['three', 'two', 'one']iterkeys方法:threetwoone>>>

3.8 pop函數:刪除字典中對應的鍵

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3}print dd.pop('one')print d

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 1}{'three': 3, 'two': 2}>>>

3.9 popitem函數:移出字典中的項

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3}print dd.popitem()print d

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 1}{'two': 2, 'one': 1}>>>

3.10 setdefault函數:類似于get方法,獲取與給定鍵相關聯的值,也可以在字典中不包含給定鍵的情況下設定相應的鍵值

# _*_ coding:utf-8 _*_d={'one':1,'two':2,'three':3}print dprint d.setdefault('one',1)print d.setdefault('four',4)print d

運算結果:

{'three': 3, 'two': 2, 'one': 1}14{'four': 4, 'three': 3, 'two': 2, 'one': 1}>>>

3.11 update函數:用一個字典更新另外一個字典

# _*_ coding:utf-8 _*_d={  'one':123,  'two':2,  'three':3  }print dx={'one':1}d.update(x)print d

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py======={'three': 3, 'two': 2, 'one': 123}{'three': 3, 'two': 2, 'one': 1}>>>

3.12 values和itervalues函數:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由于在字典中值不是唯一的,所以列表中可以包含重復的元素

# _*_ coding:utf-8 _*_d={  'one':123,  'two':2,  'three':3,  'test':2  }print d.values()

運算結果:

=======RESTART: C:/Users/Mr_Deng/Desktop/test.py=======[2, 3, 2, 123]>>>

希望本文所述對大家Python程序設計有所幫助。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 门头沟区| 青川县| 石嘴山市| 平乡县| 利辛县| 淳化县| 赤峰市| 乐清市| 静宁县| 白银市| 昌江| 淮安市| 百色市| 宣化县| 晋州市| 肥东县| 闽侯县| 综艺| 定陶县| 慈利县| 巴青县| 秀山| 鹤庆县| 北京市| 福贡县| 牟定县| 桑日县| 濮阳县| 宝应县| 徐州市| 高碑店市| 读书| 甘泉县| 盐城市| 安宁市| 博乐市| 容城县| 桓仁| 凤山县| 乌拉特后旗| 闽清县|