Python中字典的基本知識初步介紹

來源:互聯網
上載者:User

Python中字典的基本知識初步介紹

   這篇文章主要介紹了Python中字典的基本知識初步介紹,是Python入門中的基礎知識,需要的朋友可以參考下

  字典是可變的,並且可以儲存任意數量的Python對象,包括其他容器類型另一個容器類型。字典包括鍵對(稱為項目)及其相應的值。

  Python字典也被稱為關聯陣列或雜湊表。字典的一般文法如下:

  ?

1

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

  可以用下面的方式建立字典:

  ?

1

2

dict1 = { 'abc': 456 };

dict2 = { 'abc': 123, 98.6: 37 };

  每個按鍵都來自它的值用冒號(:),該項目以逗號分隔,整個事情是包含在大括弧分隔。沒有任何項目一個空的字典是寫只有兩個大括弧,就像這樣:{}

  鍵在一個字典中是唯一的,而值可能不是。字典的值可以是任何類型的,但鍵必須是不可變的資料類型,例如字串,數字,或元組。

  訪問字典的值:

  要訪問字典元素,您可以使用熟悉的方括弧一起的關鍵,獲得它的值。下面是一個簡單的例子:

  ?

1

2

3

4

5

6

#!/usr/bin/python

 

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

 

print "dict['Name']: ", dict['Name'];

print "dict['Age']: ", dict['Age'];

  當執行上面的代碼中,產生以下結果:

  ?

1

2

dict['Name']: Zara

dict['Age']: 7

  如果要訪問一個不存在的鍵,這會得到一個錯誤,如下所示:

  ?

1

2

3

4

5

#!/usr/bin/python

 

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

 

print "dict['Alice']: ", dict['Alice'];

  當執行上面的代碼,產生以下結果:

  ?

1

2

3

4

5

dict['Zara']:

Traceback (most recent call last):

File "test.py", line 4, in <module>

print "dict['Alice']: ", dict['Alice'];

KeyError: 'Alice'

  更新字典:

  可以通過添加一個新條目或項目(即一個鍵 - 值對),修改現有條目或刪除。作為簡單的例子,如所示在現有條目更新字詞:

  ?

1

2

3

4

5

6

7

8

9

10

#!/usr/bin/python

 

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

 

dict['Age'] = 8; # update existing entry

dict['School'] = "DPS School"; # Add new entry

 

 

print "dict['Age']: ", dict['Age'];

print "dict['School']: ", dict['School'];

  當執行上面的代碼,產生以下結果:

  ?

1

2

dict['Age']: 8

dict['School']: DPS School

  刪除字典元素:

  可以刪除單個字典元素或清除字典中的全部內容。也可以刪除整個字典在一個單一的操作。

  要刪除整個字典,只要用del語句。下面是一個簡單的例子:

  ?

1

2

3

4

5

6

7

8

9

10

#!/usr/bin/python

 

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

 

del dict['Name']; # remove entry with key 'Name'

dict.clear(); # remove all entries in dict

del dict ; # delete entire dictionary

 

print "dict['Age']: ", dict['Age'];

print "dict['School']: ", dict['School'];

  這將產生以下結果。注意引發異常,這是因為經過del dict刪除,字典已經不存在了:

  ?

1

2

3

4

5

dict['Age']:

Traceback (most recent call last):

File "test.py", line 8, in <module>

print "dict['Age']: ", dict['Age'];

TypeError: 'type' object is unsubscriptable

  註:del()方法會在後續的章節中討論。

  字典的鍵的屬性:

  字典值沒有限制。它們可以是任意Python對象,無論是標準的對象或使用者定義物件。但是作為鍵,是不可以這樣的。

  要記住字典中的鍵的兩個要點:

  (一)不準一個鍵對應多個條目。這意味著不能有重複的鍵。當有重複的鍵,在分配過程中以最後分配的為準。下面是一個簡單的例子:

  ?

1

2

3

4

5

#!/usr/bin/python

 

dict = {'Name': 'Zara', 'Age': 7, 'Name': 'Manni'};

 

print "dict['Name']: ", dict['Name'];

  當執行上面的代碼,產生以下結果:

  ?

1

dict['Name']: Manni

  (二)鍵的值字必須是不可變的。這意味著可以使用字串,數字或元組作為字典的鍵,但像['key']是不允許的。下面是一個簡單的例子:

  ?

1

2

3

4

5

#!/usr/bin/python

 

dict = {['Name']: 'Zara', 'Age': 7};

 

print "dict['Name']: ", dict['Name'];

  當執行上面的代碼,產生以下結果:

  ?

1

2

3

4

Traceback (most recent call last):

File "test.py", line 3, in <module>

dict = {['Name']: 'Zara', 'Age': 7};

TypeError: list objects are unhashable

聯繫我們

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