python 字典生成樹(shù)狀圖的實(shí)例
更新時(shí)間:2022年07月16日 17:08:02 作者:num270710
這篇文章主要介紹了python 字典生成樹(shù)狀圖的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
python字典生成樹(shù)狀圖
from graphviz import Digraph
# 獲取所有節(jié)點(diǎn)中最多子節(jié)點(diǎn)的葉節(jié)點(diǎn)
def getMaxLeafs(myTree):
numLeaf = len(myTree.keys())
for key, value in myTree.items():
if isinstance(value, dict):
sum_numLeaf = getMaxLeafs(value)
if sum_numLeaf > numLeaf:
numLeaf = sum_numLeaf
return numLeaf
def plot_model(tree, name):
g = Digraph("G", filename=name, format='png', strict=False)
first_label = list(tree.keys())[0]
g.node("0", first_label)
_sub_plot(g, tree, "0")
leafs = str(getMaxLeafs(tree) // 10)
g.attr(rankdir='LR', ranksep=leafs)
g.view()
root = "0"
def _sub_plot(g, tree, inc):
global root
first_label = list(tree.keys())[0]
ts = tree[first_label]
for i in ts.keys():
if isinstance(tree[first_label][i], dict):
root = str(int(root) + 1)
g.node(root, list(tree[first_label][i].keys())[0])
g.edge(inc, root, str(i))
_sub_plot(g, tree[first_label][i], root)
else:
root = str(int(root) + 1)
g.node(root, tree[first_label][i])
g.edge(inc, root, str(i))
tree = {
"tearRate": {
"reduced": "no lenses",
"normal": {
"astigmatic": {
"yes": {
"prescript": {
"myope": "hard",
"hyper": {
"age": {
"young": "hard",
"presbyopic": "no lenses",
"pre": "no lenses"
}
}
}
},
"no": {
"age": {
"young": "soft",
"presbyopic": {
"prescript": {
"myope": "no lenses",
"hyper": "soft"
}
},
"pre": "soft"
}
}
}
}
}
}
plot_model(tree, "tree.gv")效果如下:

python生成樹(shù)結(jié)構(gòu)
# 生成樹(shù)結(jié)構(gòu)
def get_trees(data,
key_column='elementId',
parent_column='parentId',
child_column='children'):
"""
:param data: 數(shù)據(jù)列表
:param key_column: 主鍵字段,默認(rèn)id
:param parent_column: 父ID字段名,父ID默認(rèn)從0開(kāi)始
:param child_column: 子列表字典名稱(chēng)
:return: 樹(shù)結(jié)構(gòu)
"""
data_dic = {}
for d in data:
data_dic[d.get(key_column)] = d # 以自己的權(quán)限主鍵為鍵,以新構(gòu)建的字典為值,構(gòu)造新的字典
data_tree_list = [] # 整個(gè)數(shù)據(jù)大列表
for d_id, d_dic in data_dic.items():
pid = d_dic.get(parent_column) # 取每一個(gè)字典中的父id
if not pid: # 父id=0,就直接加入數(shù)據(jù)大列表
data_tree_list.append(d_dic)
else: # 父id>0 就加入父id隊(duì)對(duì)應(yīng)的那個(gè)的節(jié)點(diǎn)列表
try: # 判斷異常代表有子節(jié)點(diǎn),增加子節(jié)點(diǎn)列表=[]
data_dic[pid][child_column].append(d_dic)
except KeyError:
data_dic[pid][child_column] = []
data_dic[pid][child_column].append(d_dic)
return data_tree_list
def recursion(data, l=None):
if l is None:
l = []
for i in data:
if 'children' in i:
children=i.pop('children')
l.append(i)
recursion(children,l)
else:
l.append(i)
return l以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?遞歸式實(shí)現(xiàn)二叉樹(shù)前序,中序,后序遍歷
這篇文章主要介紹了Python?遞歸式實(shí)現(xiàn)二叉樹(shù)前序,中序,后序遍歷,更多相關(guān)資料,需要的小伙伴可以參考下面具體的文章內(nèi)容2022-03-03
python按修改時(shí)間順序排列文件的實(shí)例代碼
這篇文章主要介紹了python按修改時(shí)間順序排列文件的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07
利用PyQt5+Matplotlib 繪制靜態(tài)/動(dòng)態(tài)圖的實(shí)現(xiàn)代碼
這篇文章主要介紹了利用PyQt5+Matplotlib 繪制靜態(tài)/動(dòng)態(tài)圖的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
重寫(xiě)django的model下的objects模型管理器方式
這篇文章主要介紹了重寫(xiě)django的model下的objects模型管理器方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python發(fā)送郵件的幾種方式(最全總結(jié)!)
發(fā)送電子郵件是個(gè)很常見(jiàn)的開(kāi)發(fā)需求,平時(shí)如果有什么重要的信息怕錯(cuò)過(guò),就可以發(fā)個(gè)郵件到郵箱來(lái)提醒自己,這篇文章主要給大家介紹了關(guān)于Python發(fā)送郵件的幾種方式,需要的朋友可以參考下2024-03-03
pycharm遠(yuǎn)程連接服務(wù)器運(yùn)行pytorch的過(guò)程詳解
這篇文章主要介紹了在Linux環(huán)境下使用Anaconda管理不同版本的Python環(huán)境,并通過(guò)PyCharm遠(yuǎn)程連接服務(wù)器來(lái)運(yùn)行PyTorch的過(guò)程,包括安裝PyTorch、CUDA以及配置PyCharm遠(yuǎn)程開(kāi)發(fā)環(huán)境的詳細(xì)步驟,需要的朋友可以參考下2025-02-02

