python的dict()字典數據類型的方法詳解以及案例使用

1、以前的回顧

# int  數字
# str 字符串
# list 列表
# tuple 元組
# dict 字典python

字典中最重要的方法ide

keys()spa

values()code

items()對象

getblog

update索引

字典是以  "key":"value"  的方式存儲的字符串

一、字典裏面是鍵值對,每一個元素之間也是用逗號分隔,是用{}大括號括起來的get

字典裏面的value能夠是任何值,能夠無限嵌套列表和元組hash

布爾值(不能跟0或1一塊兒出現,當key裏面沒有0和1就能夠作key)、列表、字典不能做爲字典的key

字典是無序的,每次運行,輸出的結果都不是固定的

 

2、下面瞭解下字典的經常使用操做

info = {"k1": "v1", "k2": "v2" } print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k1': 'v1', 'k2': 'v2'} info = { "k1": 18, "k2": True, "k3":[ 11, [], (), 22, 33, { "kk1":"vv1", "kk2":"vv2", "kk3":(11,22,), } ], "k4":(11,22,33,44,) } print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': True, 'k1': 18, 'k3': [11, 22, 33, {'kk3': (11, 22), 'kk2': 'vv2', 'kk1': 'vv1'}], 'k4': ()}
View Code

2.數字、字符串、布爾值、元組、均可以作key

info = { 2:"abc", "k1":"def", True:"123", (11,22):"456" } print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k1': 'def', True: '123', 2: 'abc', (11, 22): '456'}
View Code 

3.列表和字典都不能做爲字典的key

    (1)列表不能做爲字典的key

info = { 2:"abc", "k1":"def", True:"123", [11,22]:"456" } TypeError: unhashable type: 'list'
View Code

    (2)字典不能做爲字典的key

info = { 2:"abc", "k1":"def", True:"123", {"k3":"v3"}:"456" } TypeError: unhashable type: 'dict'
View Code

四、字典去保存的時候是按照哈希表保存的

五、字典是無序的(發現每次打印結果都不同,因此字典是無序的)

第一次打印

C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py

{'k2': True, 'k4': (11, 22, 33, 44), 'k1': 18, 'k3': [11, [], (), 22, 33, {'kk2': 'vv2', 'kk1': 'vv1', 'kk3': (11, 22)}]}

第二次打印

C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py

{'k4': (11, 22, 33, 44), 'k1': 18, 'k3': [11, [], (), 22, 33, {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k2': True}

 

6.按照key去取值,好比要取到18這個值應該怎麼作(這裏就不是索引的的0123這些了)

info = { "k1": 18, "k2": True, "k3":[ 11, [], (), 22, 33, { "kk1":"vv1", "kk2":"vv2", "kk3":(11,22,), } ], "k4":(11,22,33,44,) } v = info["k1"] print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 18
View Code

好比想要拿到99999這個值,就是下面這樣寫了(由於想拿3這個key對應的value,因此直接就寫3這個數字就能夠了,不用帶雙引號)

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } v = info[3] print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 99999
View Code

七、拿到11 這個元素該怎麼寫

第一步拿到k3這個key,結果就是一個列表了,想要拿到元組裏面的那個11,就得先拿列表裏面的整個字典的元素,位置是5

v = info["k3"] print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py [11, [], (), 22, 33, {'kk3': (11, 22), 'kk2': 'vv2', 'kk1': 'vv1'}]
View Code

拿到列表裏面的這個字典以後,要想取到元組裏面的11,首先得拿到kk3這個key對應的value

v = info["k3"][5] print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'kk2': 'vv2', 'kk3': (11, 22), 'kk1': 'vv1'}

如今拿到這個元組了,想要元組裏面的11這個元素就簡單了,11位於這個元素的第0個位置

v = info["k3"][5]["kk3"] print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py (11, 22)

拿到元組中的11的這個元素了

v = info["k3"][5]["kk3"][0] print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 11

 

合起來就是下面這

 

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } v = info["k3"][5]["kk3"][0] print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 11

8.刪除字典中的key

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } del info["k1"] print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {3: 99999, 'k3': [11, [], (), 22, 33, {'kk1': 'vv1', 'kk2': 'vv2', 'kk3': (11, 22)}], 'k4': (11, 22, 33, 44)}
View Code

9.刪除kk1這個key

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } del info["k3"][5]["kk1"] print (info) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k1': 18, 'k3': [11, [], (), 22, 33, {'kk3': (11, 22), 'kk2': 'vv2'}], 3: 99999, 'k4': (11, 22, 33, 44)}
View Code

十、字典支持for循環,默認循環的時候只有key

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } for n in info: print (n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py k1 3 k3 k4
View Code

11.取字典裏面的key,字典裏面也有一個方法叫keys

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } # for n in info: # print (n)
v = info.keys() print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py dict_keys(['k1', 3, 'k4', 'k3'])
View Code

12.取字典裏面的values   有一個方法叫作value,能夠結合一下for循環,一個一個的打印出來,爲啥只有四行呢,由於key只有4個,這是對應的4個key的4個value

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } for n in info.values(): print (n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 99999 [11, [], (), 22, 33, {'kk3': (11, 22), 'kk2': 'vv2', 'kk1': 'vv1'}] (11, 22, 33, 44) 18
View Code

13.既想拿到key 又想拿到value(本身寫for循環)

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } for n in info.keys(): print (n,info[n]) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py k4 (11, 22, 33, 44) k3 [11, [], (), 22, 33, {'kk2': 'vv2', 'kk3': (11, 22), 'kk1': 'vv1'}] 3 99999 k1 18
View Code

1四、既拿到key又拿到value,字典有一個方法,items  咱們用一下試試

由於是2個值,因此循環的時候,要用兩個變量,這樣items這個方法纔會把key的值賦值給k,把value的值賦值給v,最後在打印k,v

info = { "k1": 18, 3: 99999, "k3": [ 11, [], (), 22, 33, { "kk1": "vv1", "kk2": "vv2", "kk3": (11, 22,), } ], "k4": (11, 22, 33, 44,) } for k,v in info.items(): print (k,v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py k1 18 k4 (11, 22, 33, 44) 3 99999 k3 [11, [], (), 22, 33, {'kk2': 'vv2', 'kk1': 'vv1', 'kk3': (11, 22)}]
View Code

2、dict字典的方法詳解

一、clear(self)

D.clear() -> None.  Remove all items from D.

清空字典

 

D = {"11":"aa","22":"bb","33":"cc"} v = D.clear() print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py None
View Code

2. copy(self)  淺拷貝

D = {"11":"aa","22":"bb","33":"cc"} v = D.copy() print (v) print (D) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'22': 'bb', '11': 'aa', '33': 'cc'} {'22': 'bb', '11': 'aa', '33': 'cc'}
View Code

3. fromkeys

根據序列,建立字典,並指定統一一個value做爲key的值

Returns a new dict with keys from iterable and values equal to value

@staticmethod # known case   這是靜態方法的意
def fromkeys(*args, **kwargs)

靜態方法是這樣使用的  類名.方法

這個必須傳2個參數,第一個參數是用來建立字典的key的,必須是可迭代對象,列表、元組,字符串均可以,第二個參數是這些key的統一的值

 

第一個參數是列表的方式

v = dict.fromkeys(["name","11","woai"],123) print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'woai': 123, '11': 123, 'name': 123}
View Code 

第一個參數是元組的方式

v = dict.fromkeys(("what","66","woai"),123) print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'woai': 123, '66': 123, 'what': 123}
View Code

第一個參數是字符串的方式

v = dict.fromkeys("name",123) print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'m': 123, 'e': 123, 'n': 123, 'a': 123}
View Code
4. get(self, k, d=None
獲取key的值

D.get(k[,d]) -> D[k] if k in D, else d.  d defaults to None.

 

咱們經過這種方法的話,若是key不存在的話,程序就直接報錯了

D = {"11":"aa","22":"bb","33":"cc"}
v = D["111"]
print (v)

KeyError: '111'

 

get這個方法在取值的時候,直接加key的名字就能夠取到key的對應的值

 

D = { "k1":"v1", "k2":"v2" } v = D.get("k1") print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py v1
View Code

當要取的這個key不存在時,能夠指定一個返回值,默認是None

D = { "k1":"v1", "k2":"v2" } v = D.get("k3") print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py None

當要取的這個key不存在時,指定一個固定的返回值

D = { "k1":"v1", "k2":"v2" } v = D.get("k3","000") print(v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py 000

5. items(self)

同時拿到key和value
D = { "k1":"v1", "k2":"v2" } for n in D.items(): print(n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py ('k2', 'v2') ('k1', 'v1')

6. keys(self)

取出字典中的全部key

D = { "k1":"v1", "k2":"v2" } for n in D.keys(): print(n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py k2 k1

7. values(self)

取出字典裏面的全部value

D = { "k1":"v1", "k2":"v2" } for n in D.values(): print(n) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py v2 v1

8. pop(self, k, d=None)

刪除指定的key

D = { "k1":"v1", "k2":"v2" } v = D.pop("k1") print (D) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2'}

 

刪除指定的key並把刪除的value打印出來

 

D = { "k1":"v1", "k2":"v2" } v = D.pop("k1") v就是指定刪除的key 值是v1 print (D,v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2'} v1

當刪除的指定的key不存在的時候,能夠指定一個參數,讓結果返回這個指定的參數

D = { "k1":"v1", "k2":"v2" } v = D.pop("k3","000") print (D) print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k1': 'v1', 'k2': 'v2'} 000

9.popitem(self)

隨機刪除一個key

被刪除的key和value是以一個元組的方式輸出的 

D = { "k1":"v1", "k2":"v2" } v = D.popitem() print (D) print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2'} ('k1', 'v1')

也能夠這樣寫

D = { "k1":"v1", "k2":"v2" } k,v = D.popitem() print (D,k,v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2'} k1 v1

10. setdefault(self, k, d=None)

設置key對應的值,

若是參數裏面的key已存在,則不設置,獲取當前key對應的值

若是參數裏面的key不存在,則設置key的值,獲取當前key對應的值

 

當key存在的時候,則不設置,而且獲取當前key的值

D = { "k1":"v1", "k2":"v2" } v = D.setdefault("k1","woaini") print (D) print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2', 'k1': 'v1'} v1

 

當key不存在的時候,則增長一個key,並設置key的值,並返回參數中指定key的值

D = { "k1":"v1", "k2":"v2" } v = D.setdefault("k3","woaini") print (D) print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k3': 'woaini', 'k2': 'v2', 'k1': 'v1'} woaini

11. update(self, E=None, **F)

若是key存在的話,就更新key的值,若是key不存在的話,就增長到字典裏面

k1是在原來基礎上修改的,k3是新增的

 

D = { "k1":"v1", "k2":"v2" } D.update({"k1":"333","k3":"666"}) print (D) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k3': '666', 'k2': 'v2', 'k1': '333'}

第二種寫法:

 

D = { "k1":"v1", "k2":"v2" } D.update(k1=111,k3=666,k5="woaini") print (D) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py {'k2': 'v2', 'k5': 'woaini', 'k1': 111, 'k3': 666}

 

D.update(k1=111,k3=666,k5="woaini")

 

至關於在內部自動轉換成一個字典,作個更新

若是參數裏面是**kwargs  這樣的,默認就會把

D.update(k1=111,k3=666,k5="woaini") 自動轉換成字典

12.字典裏面的in操做

(1)判斷字典的key裏面是否包含k1

只能判斷key不能判斷vlaue,想判斷value怎麼辦呢,看(2)

D = { "k1":"v1", "k2":"v2" } v = "k1" in D print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py True

 

(2)判斷字典裏面的value是否包含v1

D = { "k1":"v1", "k2":"v2" } v = "v1" in D.values() print (v) C:\python35\python3.exe D:/pyproject/day12列表/dict-way.py True
相關文章
相關標籤/搜索