Python基本數據類型之字典

一、Python字典的定義和特性

1、定義:

(1)、字典是另一種可變容器模型,且可存儲任意類型對象。

(2)、字典的每個鍵值(key=>value)對用冒號(:)分割,每個對之間用逗號(,)分割,整個字典包括在花括號({})中 ,格式如下所示:

d = {key1 : value1, key2 : value2 ... }

字典定義示例:

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

2、特性:

(1)、鍵必須是唯一的,不可變的,可以用數字,字符串,布爾值或元組充當,用列表、字典就不行;值可以取任何數據類型

(2)、字典值可以是任何的 python 對象,既可以是標準的對象,也可以是用戶定義的,但鍵不行

(3)、不允許同一個鍵出現兩次。創建時如果同一個鍵被賦值兩次,只會取最後一個的值

(4)、字典是無序的

 

二、字典的 增、刪、改、查 操作

1、增:

2、刪:

(1)、單個鍵刪除:del dict[鍵]    

(2)、清空字典:dict.clear()

(3)、刪除字典:del dict

3、改:dict[鍵] = 新值

4、查:dict[鍵]來獲取對應的值,鍵必須是存在的,否則會報錯

 

三、字典內置函數&方法

1、len(dict):計算字典元素個數,即鍵的總數

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict中的元素個數爲:", len(testdict))

輸出結果:

 

2、str(dict):輸出字典,以可打印的字符串表示。

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict轉換成字符串後的樣式爲:", str(testdict))

輸出結果:

 

3、type(variable):返回輸入的變量類型,如果變量是字典就返回字典類型。

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict的類型爲:", type(testdict))

輸出結果:

 

四、字典dict類中的方法:

1、copy(self):返回一個字典的淺複製

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
new_dict = testdict.copy()
print("新拷貝的字典內容是:", new_dict)

輸出結果:

2、fromkeys(*args, **kwargs):創建一個新字典,以序列seq中元素做字典的鍵,val爲字典所有鍵對應的初始值

代碼示例:

new_dict1 = dict.fromkeys(["hehe", "haha", "heihei"], 123)
print("新創建的字典內容爲:", new_dict1)

輸出結果:

3、get(self, k, d=None):根據指定鍵獲取對應的值,如果鍵不在字典中,如果指定返回值則返回指定值,如果沒有則返回None

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
key1 = testdict.get("Name")
print("獲取到key1的值爲:", key1)
key2 = testdict.get("hahaha")
print("獲取到key2的值爲:", key2)

輸出結果:

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
key1 = testdict.get("Name")
print("獲取到key1的值爲:", key1)
key2 = testdict.get("hahaha", 222)
print("獲取到key2的值爲:", key2)

輸出結果:

 

4、items(self):以列表返回可遍歷的(鍵, 值) 元組數組

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
for itemkey, itemvalue in testdict.items():
    print("testdict的鍵是:", itemkey, "對應的值爲:", itemvalue)

輸出結果:

 

5、keys(self):返回字典中所有的鍵的一個列表

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
for item in testdict.keys():
    print("當前字典的鍵爲:", item)

輸出結果:

 

6、values(self):返回字典中所有的值的一個列表

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
for item in testdict.values():
    print("當前字典的值爲:", item)

輸出結果:

 

7、pop(self, k, d=None):刪除字典給定鍵 key 所對應的值,返回值爲被刪除的值。key值必須給出。 否則,返回default值。

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict當前字典的樣式爲:", testdict)
tempdel = testdict.pop("Class")
print("刪除的值爲:", tempdel, "\ntestdict刪除鍵Class後的樣式爲:", testdict)

輸出結果:

 

8、popitem(self):隨機返回並刪除字典中的一對鍵和值(一般刪除末尾對)。

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict當前字典的樣式爲:", testdict)
tempdel = testdict.popitem()
print("刪除的值爲:", tempdel, "\ntestdict隨機刪除後的樣式爲:", testdict)

輸出結果:

 

9、setdefault(self, k, d=None):和get()類似, 但如果鍵不存在於字典中,將會添加鍵並將值設爲default

(1)、如果修改的鍵存在,則返回字典中原本的值

代碼示例;

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict當前字典的樣式爲:", testdict)
tempvalue = testdict.setdefault("Age", 15)
print("修改後的值tempvalue=", tempvalue, "\ntestdict修改後的樣式爲:", testdict)

輸出結果;

(2)、如果修改的鍵不存在,則會將新添加的鍵值對添加到字典中

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict當前字典的樣式爲:", testdict)
tempvalue = testdict.setdefault("Number", 13512345678)
print("修改後的值tempvalue=", tempvalue, "\ntestdict修改後的樣式爲:", testdict)

輸出結果:

10、update(self, E=None, **F):把字典dict2的鍵/值對更新到dict裏,鍵存在的,更新值,鍵不存在的,添加鍵值對到字典中

寫法1代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict當前字典的樣式爲:", testdict)
testdict.update({'Age':15, "Number":135634654})
print("testdict更新後的樣式爲;", testdict)

輸出結果:

寫法2代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict當前字典的樣式爲:", testdict)
testdict.update(Age=15, Number=124152363)
print("testdict更新後的樣式爲;", testdict)

輸出結果:

11、in:判斷某個鍵是否在字典中

代碼示例:

testdict = {'Name': 'wangdoudou', 'Age': 7, 'Class': 'First'}
print("testdict當前字典的樣式爲:", testdict)
v1 = "Name" in testdict
print("Name鍵在字典testdict中:", v1)
v2 = "Number" in testdict
print("Number鍵在字典testdict中:", v2)

輸出結果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章