python 字典 dictionary

來源:互聯網
上載者:User

標籤:指定   完成   地址   ict   python 字典   類型   計算   size   數字   

Dictionary 字典

字典是python中唯一的映射類型,採用索引值對(key-value)的形式儲存資料。python對key進行雜湊函數運算,根據計算的結果決定value的儲存地址,所以字典是無序儲存的,且key必須是可雜湊的。可雜湊表示key必須是不可變類型,如:數字、字串、元祖。

字典是除列表外python中最靈活的內建資料結構類型。列表是有序的對象結合,字典是無序的對象集合。兩者之間的區別在於:字典當中的元素是通過建來存取的,而不是通過偏存取。

建立字典:

>>> dic1={‘name‘:‘alex‘,‘age‘:‘27‘,‘sex‘:‘male‘}
>>> dic2=dict(((‘name‘,‘alex‘),(‘age‘,‘27‘)))
>>> print dic1
{‘age‘: ‘27‘, ‘name‘: ‘alex‘, ‘sex‘: ‘male‘}
>>> print dic2
{‘name‘: ‘alex‘,‘age‘:‘27‘}
>>>

備忘:索引值對的key必須要是不可改變的資料類型,而value則可以是不同的資料類型,如數字,字串,列表,元祖,甚至包括字典等。

不可變資料類型:整型、字串、元祖

可變資料類型:列表、字典

操作字典:

>>> dic1={‘name‘:‘alex‘,‘age‘:‘27‘,‘sex‘:‘male‘}

新增記錄:

>>> dic1["job"]=‘it‘        向字典中添加新的索引值對
>>> print dic1
{‘job‘: ‘it‘, ‘age‘: ‘27‘, ‘name‘: ‘alex‘, ‘sex‘: ‘male‘}

 

>>> dic1.setdefault(‘age‘,34)    使用setdefault函數,如果字典中已經存在age記錄,則此命令不生效,也不會修改已經存在的索引值對。如果不存在此key,則添加到字典中。
‘27‘                setdefault函數執行完成之後,預設會有傳回值輸出,這個值就是字典中對應的key的valule。
>>> print dic1
{‘job‘: ‘it‘, ‘age‘: ‘27‘, ‘name‘: ‘alex‘, ‘sex‘: ‘male‘}

查詢字典:

>>> print dic1    
{‘job‘: ‘it‘, ‘age‘: ‘27‘, ‘name‘: ‘alex‘, ‘sex‘: ‘male‘}
>>> dic1["name"]    通過字典中的key來查詢對應的valule值。
‘alex‘

>>> dic1.has_key("age")    has_key()方法用來查詢子弟愛你中是否存在這個key,傳回值為False/True 。
True

>>> dic1.items()      items()方法用來擷取字典中所有的索引值對。
[(‘job‘, ‘it‘), (‘age‘, ‘27‘), (‘name‘, ‘alex‘), (‘sex‘, ‘male‘)]

>>> dic1.keys()      keys()方法用來擷取字典中所有的keys。
[‘job‘, ‘age‘, ‘name‘, ‘sex‘]

>>> dic1.values()      values()方法用來擷取字典中所有的values。
[‘it‘, ‘27‘, ‘alex‘, ‘male‘]

>>> dic1.clear()      clear()方法用來清空字典資料。

>>>dic1.get(‘age‘)       get()方法用於擷取字典中key對應的值,如果沒有這個key,也不會給出任何報錯。

執行個體:

>>> for key,value in dic1.items():    for迴圈可以指定兩個變數,這樣key可以擷取字典對應的key,value可以擷取字典對應的values。
... print(key,value)
...
(‘job‘, ‘it‘)
(‘age‘, ‘27‘)
(‘name‘, ‘alex‘)
(‘sex‘, ‘male‘)

修改字典記錄:

>>> dic1["age"]=34      修改字典key對應的values值,和列表,元祖等一樣,直接對應的修改即可。
>>> print(dic1)
{‘job‘: ‘it‘, ‘age‘: 34, ‘name‘: ‘alex‘, ‘sex‘: ‘male‘}

 

>>> dic2[‘age‘]=38
>>> dic2
{‘age‘: 38, ‘name‘: ‘alex‘}      age=38
>>> dic1
{‘job‘: ‘it‘, ‘name‘: ‘alex‘, ‘age‘: ‘27‘, ‘sex‘: ‘male‘}      age=27
>>> dic1.update(dic2)        update()方法會將dic2裡面的資料更新到dic1中,如果有相同的key,dic2的的值會覆蓋dic1裡面的值。
>>> dic1
{‘job‘: ‘it‘, ‘name‘: ‘alex‘, ‘age‘: 38, ‘sex‘: ‘male‘}    執行完update之後,dic1裡面的age對應的value值變成了38。
>>> dic2
{‘age‘: 38, ‘name‘: ‘alex‘}      dic2中的age值沒有發生變化。

刪除字典記錄:

>>> del dic1[‘name‘]    del刪除dic1中的name記錄,del刪除的是索引值對。

>>> del dic1      這裡是刪除整個字典,從記憶體中刪除。

>>> dic1.clear()      clear()用來清空字典,返回一個空字典。

>>> dic1.pop(‘age‘)    pop()方法用來刪除字典中的一個key,pop方法有個傳回值,這裡返回的是age對應的values值。

>>> dic1.popitem()    popitem()刪除的是字典中的第一個索引值對,沒有參數,這裡的傳回值是一組索引值對。

 

python 字典 dictionary

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.