項目:
基于Pymysql的專家隨機抽取系統
引入庫函數:
>>> import treelib >>> from treelib import Tree, Node
構造節點類:
>>> class Nodex(object): \ def __init__(self, num): \ self.num = num
構造多叉樹: (注意節點的第2個屬性已標紅,它是節點ID,為str類型,不能與其他節點重復,否則構建節點失敗)
>>> tree1 = Tree() >>> tree1.create_node('Root', 'root', data = Nodex('3'));\ tree1.create_node('Child1', 'child1', parent = 'root', data =Nodex('4'));\ tree1.create_node('Child2', 'child2', parent = 'root', data =Nodex('5'));\ tree1.create_node('Child3', 'child3', parent = 'root', data =Nodex('6'));\
構造結果:
>>> tree1.show() Root ├── Child1 ├── Child2 └── Child3 >>> tree1.show(data_property = 'num') 3 ├── 4 ├── 5 └── 6
打印節點信息:(其實節點是以字典的形式存儲的)
>>> tree1.nodes {'root': Node(tag=Root, identifier=root, data=<__main__.Nodex object at 0x000002265C6A9550>), 'child1': Node(tag=Child1, identifier=child1, data=<__main__.Nodex object at 0x000002265C6A9E10>)}
取出child1節點存儲的數據:
>>> tree1.nodes['child1'].data.num '4'
以上這篇Python多叉樹的構造及取出節點數據(treelib)的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
