Python基礎7-字典

來源:互聯網
上載者:User

標籤:表示   http   one   list   post   列表   reference   數字   default   

字典一種key - value 的資料類型。

字典是無序的,鍵必須是唯一的,但值則不必,值可以取任何資料類型,但鍵必須是不可變的,如字串,數字或元組。

 

字典的常用操作

建立

直接建立

dict1 = {‘student1‘:‘01‘,‘color‘:[‘red‘,‘white‘],‘guangdong‘:{‘guangzhou‘:[‘haizhu‘,‘tianhe‘],‘shenzhen‘:[‘nanshan‘,‘futian‘]}}

通過dict函數建立

#通過list建立>>> student = [(‘student1‘,‘01‘),(‘student2‘,‘02‘)]>>> student_dict = dict(student)>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘}#通過關鍵字參數建立>>> student_dict = dict(student1=‘01‘,student2=‘02‘,student3=‘03‘)>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘}

  

追加

>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘}>>> student_dict[‘student4‘] = ‘04‘>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘}

 

修改

>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘}>>> student_dict[‘student4‘] = ‘004‘>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘004‘}

 

刪除

>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘004‘}>>> student_dict.pop(‘student4‘)‘004‘>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘}>>> del student_dict[‘student3‘]>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘}>>> student_dict.popitem()       #隨機刪除(‘student2‘, ‘02‘)>>> student_dict{‘student1‘: ‘01‘}

 

尋找

>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘}>>> ‘student1‘ in student_dictTrue>>> student_dict.get(‘student1‘)‘01‘>>> student_dict[‘student1‘]‘01‘>>> student_dict.get(‘student5‘)>>> student_dict[‘student5‘]      #如果一個key不存在,就報錯,get方法不會,不存在只返回None

>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘}>>> student_dict.keys()dict_keys([‘student1‘, ‘student2‘, ‘student3‘, ‘student4‘])>>> student_dict.values()dict_values([‘01‘, ‘02‘, ‘03‘, ‘04‘])>>> student_dict.items()dict_items([(‘student1‘, ‘01‘), (‘student2‘, ‘02‘), (‘student3‘, ‘03‘), (‘student4‘, ‘04‘)])>>> type(student_dict.items())<class ‘dict_items‘>>>> type(student_dict)<class ‘dict‘>
#嵌套字典尋找>>> dict1 = {‘student1‘:‘01‘,‘color‘:[‘red‘,‘white‘],‘guangdong‘:{‘guangzhou‘:[‘haizhu‘,‘tianhe‘],‘shenzhen‘:[‘nanshan‘,‘futian‘]}}>>> dict1[‘guangdong‘][‘guangzhou‘][0]‘haizhu‘

  

更新

>>> student_dict2 = {‘student5‘:‘05‘,‘student2‘:‘002‘}>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘02‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘}>>> student_dict.update(student_dict2)   #用字典student_dict2更新,更新鍵對應的新值,添加新的索引值對。>>> student_dict{‘student1‘: ‘01‘, ‘student2‘: ‘002‘, ‘student3‘: ‘03‘, ‘student4‘: ‘04‘, ‘student5‘: ‘05‘}

 

遍曆

#方法1for key in info:    print(key,info[key])#方法2for k,v in info.items(): #會先把dict轉成list,資料大時盡量不要用    print(k,v)

  

字典相關的函數

 

序號 函數及描述 執行個體
1 len(dict)
計算字典元素個數,即鍵的總數。
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> len(dict)3
2 str(dict)
輸出字典,以可列印的字串表示。
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> str(dict)"{‘Name‘: ‘Runoob‘, ‘Class‘: ‘First‘, ‘Age‘: 7}"
3 type(variable)
返回輸入的變數類型,如果變數是字典就返回字典類型。
>>> dict = {‘Name‘: ‘Runoob‘, ‘Age‘: 7, ‘Class‘: ‘First‘}>>> type(dict)<class ‘dict‘>

 

  

字典的內建方法 

 

序號 函數及描述
1 radiansdict.clear()
刪除字典內所有元素
2 radiansdict.copy()
返回一個字典的淺複製
3 radiansdict.fromkeys()
建立一個新字典,以序列seq中元素做字典的鍵,val為字典所有鍵對應的初始值
4 radiansdict.get(key, default=None)
返回指定鍵的值,如果值不在字典中返回default值
5 key in dict
如果鍵在字典dict裡返回true,否則返回false
6 radiansdict.items()
以列表返回可遍曆的(鍵, 值) 元組數組
7 radiansdict.keys()
以列表返回一個字典所有的鍵
8 radiansdict.setdefault(key, default=None)
和get()類似, 但如果鍵不存在於字典中,將會添加鍵並將值設為default
9 radiansdict.update(dict2)
把字典dict2的鍵/值對更新到dict裡
10 radiansdict.values()
以列表返回字典中的所有值
11 pop(key[,default])
刪除字典給定鍵 key 所對應的值,傳回值為被刪除的值。key值必須給出。 否則,返回default值。
12 popitem()
隨機返回並刪除字典中的一對鍵和值(一般刪除末尾對)。

 

參考連結:http://www.runoob.com/python3/python3-dictionary.html

參考連結:http://www.cnblogs.com/alex3714/articles/5717620.html

 

Python基礎7-字典

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.