網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
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: 子列表字典名稱
: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
原文鏈接:https://blog.csdn.net/num270710/article/details/103004966
相關(guān)推薦
- 2022-04-15 C語(yǔ)言的程序環(huán)境與預(yù)處理你真的了解嗎_C 語(yǔ)言
- 2022-12-19 批處理bat腳本獲取打包發(fā)布問(wèn)題記錄_DOS/BAT
- 2023-01-29 React基于路由的代碼分割技術(shù)詳解_React
- 2022-05-11 spring cloud alibaba nacos搭建最小可運(yùn)行微服務(wù)
- 2022-10-26 jQuery中DOM?屬性使用實(shí)例詳解上篇_jquery
- 2022-05-12 在pycharm中設(shè)置快速創(chuàng)建
- 2022-05-01 Python類(lèi)的定義和使用詳情_(kāi)python
- 2022-12-02 Android系統(tǒng)狀態(tài)欄定制圖標(biāo)顯示邏輯控制_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支