網站首頁 編程語言 正文
python字典生成樹狀圖
from graphviz import Digraph
# 獲取所有節點中最多子節點的葉節點
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生成樹結構
# 生成樹結構
def get_trees(data,
key_column='elementId',
parent_column='parentId',
child_column='children'):
"""
:param data: 數據列表
:param key_column: 主鍵字段,默認id
:param parent_column: 父ID字段名,父ID默認從0開始
:param child_column: 子列表字典名稱
:return: 樹結構
"""
data_dic = {}
for d in data:
data_dic[d.get(key_column)] = d # 以自己的權限主鍵為鍵,以新構建的字典為值,構造新的字典
data_tree_list = [] # 整個數據大列表
for d_id, d_dic in data_dic.items():
pid = d_dic.get(parent_column) # 取每一個字典中的父id
if not pid: # 父id=0,就直接加入數據大列表
data_tree_list.append(d_dic)
else: # 父id>0 就加入父id隊對應的那個的節點列表
try: # 判斷異常代表有子節點,增加子節點列表=[]
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
相關推薦
- 2022-12-22 Flutter組件開發過程完整講解_Android
- 2023-03-18 pandas檢查和填充缺失值的N種方法總結_python
- 2022-05-29 MVC使用MvcPager實現分頁效果_實用技巧
- 2023-07-05 【nacos優化】定時刪除access日志
- 2023-01-11 jQuery綁定點擊事件與改變事件的方式總結及多個元素綁定多個事件_jquery
- 2022-11-27 Ceph集群CephFS文件存儲核心概念及部署使用詳解_其它綜合
- 2022-04-07 使用swift實現計算器功能_Swift
- 2022-07-21 python logging模塊使用介紹
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支