怎么在python中實(shí)現(xiàn)一個(gè)樹狀嵌套結(jié)構(gòu)?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。
創(chuàng)新互聯(lián)主要從事網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)杞縣,十年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792python是什么意思Python是一種跨平臺(tái)的、具有解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計(jì)是用于編寫自動(dòng)化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。
原始數(shù)據(jù)
原始數(shù)據(jù)大致是這樣子的:
每條數(shù)據(jù)中的四個(gè)數(shù)據(jù)分別是 當(dāng)前節(jié)點(diǎn)名稱,節(jié)點(diǎn)描述(指代一些需要的節(jié)點(diǎn)屬性),源節(jié)點(diǎn)(即最頂層節(jié)點(diǎn)),父節(jié)點(diǎn)(當(dāng)前節(jié)點(diǎn)上一層節(jié)點(diǎn))。
datas = [ ["root", "根節(jié)點(diǎn)", "root", None], ["node1", "一級(jí)節(jié)點(diǎn)1", "root", "root"], ["node2", "一級(jí)節(jié)點(diǎn)2", "root", "root"], ["node11", "二級(jí)節(jié)點(diǎn)11", "root", "node1"], ["node12", "二級(jí)節(jié)點(diǎn)12", "root", "node1"], ["node21", "二級(jí)節(jié)點(diǎn)21", "root", "node2"], ["node22", "二級(jí)節(jié)點(diǎn)22", "root", "node2"], ]
節(jié)點(diǎn)類
抽象封裝出一個(gè)節(jié)點(diǎn)類:
class Node(object): def __init__(self, name: str, desc, parent: str, children: list): """ 初始化 :param name: :param desc: :param parent: :param children: """ self.name = name self.desc = desc self.parent = parent self.children = children def get_nodes(self): """ 獲取該節(jié)點(diǎn)下的全部結(jié)構(gòu)字典 """ d = dict() d['name'] = self.name d['desc'] = self.desc d['parent'] = self.parent children = self.get_children() if children: d['children'] = [child.get_nodes() for child in children] return d def get_children(self): """ 獲取該節(jié)點(diǎn)下的全部節(jié)點(diǎn)對(duì)象 """ return [n for n in nodes if n.parent == self.name] def __repr__(self): return self.name
將原始數(shù)據(jù)轉(zhuǎn)換為節(jié)點(diǎn)對(duì)象
nodes = list() for data in datas: node = Node(data[0], data[1], data[-1], []) nodes.append(node)
為各個(gè)節(jié)點(diǎn)建立聯(lián)系
for node in nodes: children_names = [data[0] for data in datas if data[-1] == node.name] children = [node for node in nodes if node.name in children_names] node.children.extend(children)
測(cè)試
root = nodes[0] print(root) tree = root.get_nodes() print(json.dumps(tree, indent=4))
運(yùn)行結(jié)果:
原始數(shù)據(jù)也可以是字典的形式:
### fork_tool.py import json class Node(object): def __init__(self, **kwargs): """ 初始化 :param nodes: 樹的全部節(jié)點(diǎn)對(duì)象 :param kwargs: 當(dāng)前節(jié)點(diǎn)參數(shù) """ self.forked_id = kwargs.get("forked_id") self.max_drawdown = kwargs.get("max_drawdown") self.annualized_returns = kwargs.get("annualized_returns") self.create_time = kwargs.get("create_time") self.desc = kwargs.get("desc") self.origin = kwargs.get("origin") self.parent = kwargs.get("parent") self.children = kwargs.get("children", []) def get_nodes(self, nodes): """ 獲取該節(jié)點(diǎn)下的全部結(jié)構(gòu)字典,即建立樹狀聯(lián)系 """ d = dict() d['forked_id'] = self.forked_id d['max_drawdown'] = self.max_drawdown d['annualized_returns'] = self.annualized_returns d['create_time'] = self.create_time d['desc'] = self.desc d['origin'] = self.origin d['parent'] = self.parent children = self.get_children(nodes) if children: d['children'] = [child.get_nodes(nodes) for child in children] return d def get_children(self, nodes): """ 獲取該節(jié)點(diǎn)下的全部節(jié)點(diǎn)對(duì)象 """ return [n for n in nodes if n.parent == self.forked_id] # def __repr__(self): # return str(self.desc) def process_datas(datas): """ 處理原始數(shù)據(jù) :param datas: :return: """ # forked_infos.append({"forked_id": str(forked_strategy.get("_id")), # "max_drawdown": max_drawdown, # "annualized_returns": annualized_returns, # "create_time": create_time, # 分支創(chuàng)建時(shí)間 # "desc": desc, # "origin": origin, # "parent": parent, # "children": [], # }) nodes = [] # 構(gòu)建節(jié)點(diǎn)列表集 for data in datas: node = Node(**data) nodes.append(node) # 為各個(gè)節(jié)點(diǎn)對(duì)象建立類 nosql 結(jié)構(gòu)的聯(lián)系 for node in nodes: children_ids = [data["forked_id"] for data in datas if data["parent"] == node.forked_id] children = [node for node in nodes if node.forked_id in children_ids] node.children.extend(children) return nodes test_datas = [ {'annualized_returns': 0.01, 'children': [], 'create_time': 1562038393, 'desc': 'root', 'forked_id': '5d1ad079e86117f3883f361e', 'max_drawdown': 0.01, 'origin': None, 'parent': None}, {'annualized_returns': 0.314, 'children': [], 'create_time': 1562060612, 'desc': 'level1', 'forked_id': '5d1b2744b264566d3f3f3632', 'max_drawdown': 0.2, 'origin': '5d1ad079e86117f3883f361e', 'parent': '5d1ad079e86117f3883f361e'}, {'annualized_returns': 0.12, 'children': [], 'create_time': 1562060613, 'desc': 'level11', 'forked_id': '5d1b2745e86117f3883f3632', 'max_drawdown': None, 'origin': '5d1ad079e86117f3883f361e', 'parent': '5d1b2744b264566d3f3f3632'}, {'annualized_returns': 0.09, 'children': [], 'create_time': 1562060614, 'desc': 'level12', 'forked_id': '5d1b2746b264566d3f3f3633', 'max_drawdown': None, 'origin': '5d1ad079e86117f3883f361e', 'parent': '5d1b2744b264566d3f3f3632'}, {'annualized_returns': None, 'children': [], 'create_time': 1562060614, 'desc': 'level2', 'forked_id': '5d1b2746e86117f3883f3633', 'max_drawdown': None, 'origin': '5d1ad079e86117f3883f361e', 'parent': '5d1ad079e86117f3883f361e'}, {'annualized_returns': None, 'children': [], 'create_time': 1562060627, 'desc': 'level21', 'forked_id': '5d1b2753b264566d3f3f3635', 'max_drawdown': None, 'origin': '5d1ad079e86117f3883f361e', 'parent': '5d1b2746e86117f3883f3633'}, {'annualized_returns': None, 'children': [], 'create_time': 1562060628, 'desc': 'level211', 'forked_id': '5d1b2754b264566d3f3f3637', 'max_drawdown': None, 'origin': '5d1ad079e86117f3883f361e', 'parent': '5d1b2753b264566d3f3f3635'}, {'annualized_returns': None, 'children': [], 'create_time': 1562060640, 'desc': 'level212', 'forked_id': '5d1b2760e86117f3883f3634', 'max_drawdown': None, 'origin': '5d1ad079e86117f3883f361e', 'parent': '5d1b2753b264566d3f3f3635'}, ] if __name__ == "__main__": nodes = process_datas(test_datas) info = nodes[0].get_nodes(nodes) print(json.dumps(info, indent=4))
關(guān)于怎么在python中實(shí)現(xiàn)一個(gè)樹狀嵌套結(jié)構(gòu)問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。