創(chuàng)新互聯(lián)www.cdcxhl.cn八線(xiàn)動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買(mǎi)多久送多久,劃算不套路!
小編給大家分享一下Python中set和dict的案例分析,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
dict常用操作
引言
clear(): 清空字典
copy(): 返回一個(gè)淺拷貝
fromkeys(): 將可迭代對(duì)象中的每一個(gè)元素作為key和同一個(gè)value拼成字典
get(): 根據(jù)key返回value,若無(wú)對(duì)應(yīng)的鍵值對(duì),則返回None,也可以指定默認(rèn)返回值,和索引訪(fǎng)問(wèn)相比,不會(huì)產(chǎn)生異常。
items():返回一個(gè)dict_items類(lèi)型,支持迭代,鍵值對(duì)以元組形式組織
setdefault(): 獲取key對(duì)應(yīng)的value值,先調(diào)用get(),若不存在該鍵值對(duì),則添加
update(): 合并字典,或鍵值對(duì)元組構(gòu)成的可迭代對(duì)象
使用案例
# 1. clear() d = {name:"MetaTian", age:"22"} d.clear() # 2. copy() new_dict = d.copy() new_dict["age"] = 18 print(new_dict) print(d) # resutl: # {'age': 18, 'name': 'MetaTian'} # {'age': '22', 'name': 'MetaTian'} # 3. fromkeys() d = dict.fromkeys(range(3), "MetaTian") print(d) # result: # {0: 'MetaTian', 1: 'MetaTian', 2: 'MetaTian'} # 4. get() print(d.get(2)) print(d.get(3)) print(d.get(3, "null")) # result: # MetaTian # None # null # 5. items() print(type(d.items())) print(d.items()) # result: # <class 'dict_items'> # dict_items([(0, 'MetaTian'), (1, 'MetaTian'), (2, 'MetaTian')]) # 6. setdefault() d = {} value = d.setdefault("name", "MetaTian") # 如果無(wú) name 這個(gè) key,則添加 print(value, d) # result: # MetaTian {'name': 'MetaTian'} # 7. update() d1 = {1:"a"} d2 = {2:"b"} d1.update(d2) d2.update([(3, "c"), (4, "d")]) print(d1) print(d2) # result: # {1: 'a', 2: 'b'} # {2: 'b', 3: 'c', 4: 'd'}
set和frozenset
引言
set是可變集合,frozenset是不可變集合
集合中的元素?zé)o序,不重復(fù)
使用案例
""" 通過(guò) set(Iterable) 來(lái)構(gòu)建出可變集合對(duì)象 通過(guò) frozenset(Iterable) 構(gòu)建不可變集合對(duì)象 """ s = set("12345666") fs = frozenset(['a', 'b', 'c', 'a']) # 不可變類(lèi)型,可以作為 dict 的 key print(s) print(fs) # result: # {'6', '1', '4', '5', '3', '2'} # frozenset({'b', 'a', 'c'}) """ 向 set 中添加元素 add() update() """ s1, s2 = set("123"), set("234") s1.update(s2) s2.add('5') print(s1) print(s2) # result: # {'1', '2', '3', '4'} # {'2', '3', '5', '4'} """ 集合的運(yùn)算 - 差 & 交 | 并 """ s1, s2 = set("123"), set("234") print(s1 - s2) print(s1 & s2) print(s1 | s2) # result: # {'1'} # {'2', '3'} # {'3', '1', '2', '4'}
dict和set的實(shí)現(xiàn)原理
引言
dict和set的查找性能遠(yuǎn)遠(yuǎn)大于list
dict和set底層通過(guò)散列表存儲(chǔ),因此也要求dict的key是可哈希的,不可變對(duì)象都是可哈希的
哈希的原理.
以字典為例.
存儲(chǔ)之前要通過(guò)哈希函數(shù)來(lái)計(jì)算key的值,得到存儲(chǔ)索引,如果得到的結(jié)果已經(jīng)被使用,要處理沖突,重新計(jì)算后再進(jìn)行存儲(chǔ)
自定義的類(lèi)通過(guò)實(shí)現(xiàn)__hash__(),就可以存儲(chǔ)在dict和set中.
看完了這篇文章,相信你對(duì)Python中set和dict的案例分析有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!
本文題目:Python中set和dict的案例分析-創(chuàng)新互聯(lián)
鏈接地址:http://www.rwnh.cn/article28/csdgcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、外貿(mào)建站、網(wǎng)站策劃、網(wǎng)站營(yíng)銷(xiāo)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容