在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Python/ Python字典
Python異常處理
Python循環(huán)
Python基本運算符
Python網絡編程(Sockets)
Python可以開發(fā)哪些程序?
Python XML解析和處理
Python數(shù)字
Python函數(shù)
Python變量類型
Python os模塊方法
Python迭代器
Python安裝和環(huán)境配置
Python構造函數(shù)
Python文件對象方法
Python日期和時間
Python的歷史
Python生成器
Python+MySQL數(shù)據(jù)庫操作(PyMySQL)
Python命令行參數(shù)
Python元組
Python發(fā)送郵件
Python列表
Python文件讀寫
Python教程
Python面向對象(類和對象)
Python多線程編程
Python多重繼承
Python決策
Python是什么?
Python快速入門
Python繼承
Python字典
Python字符串
Python操作符重載
Python正則表達式
Python閉包
Python修飾器
Python功能特點
Python模塊

Python字典

每個鍵與其值使用一個冒號(:)分開,這些鍵-值對是使用逗號分隔的,整個字典項目用大括號括起來。 沒有任何項目的空字典只用兩個花括號寫成:{}

鍵在字典中是唯一的,而值可以不必是唯一的。字典的值可以是任何類型的,但是鍵必須是不可變的數(shù)據(jù)類型,例如字符串,數(shù)字或元組。

1.訪問字典中的值

要訪問字典元素,可以使用熟悉的中括號以及鍵來獲取其值。 以下是一個簡單的例子 -

#!/usr/bin/python3

dict = {'Name': 'Maxsu', 'Age': 7, 'Class': 'First'}
print ("dict['Name']: ", dict['Name'])
print ("dict['Age']: ", dict['Age'])

當執(zhí)行上述代碼時,會產生以下結果 -

dict['Name']:  Maxsu
dict['Age']:  7

如果嘗試使用鍵(不是字典的一部分)訪問數(shù)據(jù)項,會收到以下錯誤,如下示例 -

#!/usr/bin/python3

dict = {'Name': 'Maxsu', 'Age': 7, 'Class': 'First'}
print ("dict['Minsu']: ", dict['Minsu'])

當執(zhí)行上述代碼時,會產生以下結果 -

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'Minsu'

2.更新字典

可以通過添加新數(shù)據(jù)項或鍵值對,修改現(xiàn)有數(shù)據(jù)項或刪除現(xiàn)有數(shù)據(jù)項來更新字典,如下面給出的簡單示例所示。

#!/usr/bin/python3

dict = {'Name': 'Maxsu', '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'])

當執(zhí)行上述代碼時,會產生以下結果 -

dict['Age']:  8
dict['School']:  DPS School

3.刪除詞典元素

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

要顯式刪除整個字典,只需使用del語句。 以下是一個簡單的例子 -

#!/usr/bin/python3

dict = {'Name': 'Maxsu', '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'])

這產生以下結果:程序拋出了一個例外,因為在執(zhí)行del dict之后,字典不再存在。

print ("dict['Age']: ", dict['Age'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable
>>> print ("dict['School']: ", dict['School'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'type' object is not subscriptable

注 - del()方法將在后續(xù)章節(jié)中討論。

4. 字典鍵的屬性

字典值沒有限制。它們可以是任意任意的Python對象,標準對象或用戶定義的對象。 但是,對于鍵來說也是如此。

關于字典的鍵有兩個要點:

(a). 不允許每鍵多于數(shù)據(jù)值。這意味著不允許重復的鍵。 在分配過程中遇到重復鍵時,則以最后一個賦值為準。 例如 -

#!/usr/bin/python3

dict = {'Name': 'Maxsu', 'Age': 7, 'Name': 'Minlee'}
print ("dict['Name']: ", dict['Name'])

當執(zhí)行上述代碼時,會產生以下結果 -

dict['Name']:  Minlee

(b). 鍵必須是不可變的。 這意味著可以使用字符串,數(shù)字或元組作為字典鍵,但不允許使用['key']。 以下是一個簡單的例子 -

#!/usr/bin/python3

dict = {['Name']: 'Maxsu', 'Age': 7}
print ("dict['Name']: ", dict['Name'])

當執(zhí)行上述代碼時,會產生以下結果 -

Traceback (most recent call last):
   File "test.py", line 3, in <module>
      dict = {['Name']: 'Maxsu', 'Age': 7}
TypeError: list objects are unhashable

5.內置詞典函數(shù)和方法

Python包括以下字典函數(shù) -

編號 函數(shù) 描述
1 cmp(dict1, dict2) Python 3中不再可用。
2 len(dict) 計算出字典的總長度。它將等于字典中的數(shù)據(jù)項數(shù)目。
3 str(dict) 生成字典的可打印字符串表示形式
4 type(variable) 返回傳遞變量的類型。如果傳遞變量是字典,那么它將返回一個字典類型。

Python包括以下字典方法 -

編號 函數(shù) 描述
1 dict.clear() 刪除字典dict的所有元素
2 dict.copy() 返回字典dict的淺拷貝
3 dict.fromkeys() 創(chuàng)建一個新的字典,其中包含seq的值和設置為value的值。
4 dict.get(key, default=None) 對于鍵(key)存在則返回其對應值,如果鍵不在字典中,則返回默認值
5 dict.has_key(key) 此方法已刪除,使用in操作符代替
6 dict.items() 返回字典dict(key,value)元組對的列表
7 dict.keys() 返回字典dict的鍵列表
8 dict.setdefault(key, default = None) 類似于get(),如果key不在字典dict中,則將執(zhí)行賦值操作:dict [key] = default
9 dict.update(dict2) 將字典dict2的鍵值對添加到字典dict
10 dict.values() 返回字典dict的值列表