利用Python演示數(shù)型數(shù)據(jù)結(jié)構(gòu)的教程
使用 Python 內(nèi)建的defaultdict 方法可以輕松定義一個(gè)樹(shù)的數(shù)據(jù)結(jié)構(gòu)。
簡(jiǎn)單的說(shuō)樹(shù)也可以是一個(gè)字典數(shù)據(jù)結(jié)構(gòu)
def tree(): return defaultdict(tree)
這就是全部,就一行代碼。
如果你繼續(xù)下面的代碼,需要先引入
from collections import defaultdict
實(shí)例
JSON-esque
現(xiàn)在我們創(chuàng)建一個(gè) JSON-esque 嵌套字典無(wú)需顯式的創(chuàng)建子字典:
users = tree() users['harold']['username'] = 'hrldcpr' users['handler']['username'] = 'matthandlersux'
然后可通過(guò) <code>print(json.dumps(users))</code> 來(lái)打印 JSON 數(shù)據(jù),結(jié)果如下:
{"harold": {"username": "hrldcpr"}, "handler": {"username": "matthandlersux"}}
無(wú)需賦值
我們不需要通過(guò)賦值就可以創(chuàng)建結(jié)構(gòu):
taxonomy = tree() taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Felidae']['Felis']['cat'] taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Felidae']['Panthera']['lion'] taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Canidae']['Canis']['dog'] taxonomy['Animalia']['Chordata']['Mammalia']['Carnivora']['Canidae']['Canis']['coyote'] taxonomy['Plantae']['Solanales']['Solanaceae']['Solanum']['tomato'] taxonomy['Plantae']['Solanales']['Solanaceae']['Solanum']['potato'] taxonomy['Plantae']['Solanales']['Convolvulaceae']['Ipomoea']['sweet potato']
要打印有好的信息,需要轉(zhuǎn)成標(biāo)準(zhǔn)的字典對(duì)象:
def dicts(t): return {k: dicts(t[k]) for k in t}
現(xiàn)在可通過(guò) pprint(dicts(taxonomy)) 進(jìn)行打印了:
{'Animalia': {'Chordata': {'Mammalia': {'Carnivora': {'Canidae': {'Canis': {'coyote': {},
'dog': {}}},
'Felidae': {'Felis': {'cat': {}},
'Panthera': {'lion': {}}}}}}},
'Plantae': {'Solanales': {'Convolvulaceae': {'Ipomoea': {'sweet potato': {}}},
'Solanaceae': {'Solanum': {'potato': {},
'tomato': {}}}}}}
子結(jié)構(gòu)也被當(dāng)作是字典對(duì)象了,而葉子節(jié)點(diǎn)是一個(gè)空的字典對(duì)象
迭代
可以使用有趣的方法對(duì)樹(shù)進(jìn)行迭代。
例如我們解析一個(gè)動(dòng)物的列表并添加到之前定義的 taxonomy 中,我們可以使用如下代碼:
add(taxonomy,
'Animalia,Chordata,Mammalia,Cetacea,Balaenopteridae,Balaenoptera,blue whale'.split(','))
簡(jiǎn)化實(shí)現(xiàn):
def add(t, keys): for key in keys: t = t[key]
我們?nèi)匀粺o(wú)需賦值:
{'Animalia': {'Chordata': {'Mammalia': {'Carnivora': {'Canidae': {'Canis': {'coyote': {},
'dog': {}}},
'Felidae': {'Felis': {'cat': {}},
'Panthera': {'lion': {}}}},
'Cetacea': {'Balaenopteridae': {'Balaenoptera': {'blue whale': {}}}}}}},
'Plantae': {'Solanales': {'Convolvulaceae': {'Ipomoea': {'sweet potato': {}}},
'Solanaceae': {'Solanum': {'potato': {},
'tomato': {}}}}}}
結(jié)論
上面提及的這些可能用處不大,只是做了一些有意思的代碼。
如果你喜歡 Python 的話,把這個(gè)當(dāng)成是樂(lè)趣來(lái)理解。
- python數(shù)據(jù)結(jié)構(gòu)之圖深度優(yōu)先和廣度優(yōu)先實(shí)例詳解
- python學(xué)習(xí)數(shù)據(jù)結(jié)構(gòu)實(shí)例代碼
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之快速排序詳解
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之基本搜索詳解
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之鏈表詳解
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之雙端隊(duì)列詳解
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列詳解
- Python中的高級(jí)數(shù)據(jù)結(jié)構(gòu)詳解
- python數(shù)據(jù)結(jié)構(gòu)之圖的實(shí)現(xiàn)方法
相關(guān)文章
使用Python Pandas處理億級(jí)數(shù)據(jù)的方法
這篇文章主要介紹了使用Python Pandas處理億級(jí)數(shù)據(jù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python多項(xiàng)式回歸的實(shí)現(xiàn)方法
這篇文章主要介紹了Python多項(xiàng)式回歸的實(shí)現(xiàn)方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03
Kmeans聚類算法python sklearn用戶畫(huà)像教程
這篇文章主要介紹了Kmeans聚類算法python sklearn用戶畫(huà)像教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Python實(shí)現(xiàn)爬蟲(chóng)從網(wǎng)絡(luò)上下載文檔的實(shí)例代碼
小編最近在研究python,接觸到了爬蟲(chóng),本文給大家?guī)?lái)了Python實(shí)現(xiàn)爬蟲(chóng)從網(wǎng)絡(luò)上下載文檔的知識(shí)。下面小編把具體實(shí)例代碼分享到腳本之家平臺(tái),感興趣的朋友參考下吧2018-06-06
Selenium定位元素的方法小結(jié)及語(yǔ)法詳解
Selenium是一種用于自動(dòng)化網(wǎng)頁(yè)操作的工具,通過(guò)不同定位策略可以精準(zhǔn)定位網(wǎng)頁(yè)元素,本文介紹了8種定位方法,并詳細(xì)說(shuō)明了每種方法的語(yǔ)法、使用場(chǎng)景及實(shí)際操作中的注意事項(xiàng),需要的朋友可以參考下2025-02-02
使用Python編程分析火爆全網(wǎng)的魷魚(yú)游戲豆瓣影評(píng)
本文來(lái)為大家介紹如何使用Python爬取影評(píng)的操作,主要是爬取《魷魚(yú)游戲》在豆瓣上的一些影評(píng),對(duì)數(shù)據(jù)做一些簡(jiǎn)單的分析,用數(shù)據(jù)的角度重新審視下這部劇,有需要的朋友可以借鑒參考下2021-10-10

