校园网上零售网站建设方案,在线免费看1921完整版,wordpress+python导入,研发外包树是计算机科学中重要的数据结构。例如决策树等机器学习算法设计、文件系统索引等。创建treelib包是为了在Python中提供树数据结构的有效实现。
Treelib的主要特点包括#xff1a;
节点搜索的高效操作。支持常见的树操作#xff0c;如遍历、插入、删除、节点移动、浅/深复制…树是计算机科学中重要的数据结构。例如决策树等机器学习算法设计、文件系统索引等。创建treelib包是为了在Python中提供树数据结构的有效实现。
Treelib的主要特点包括
节点搜索的高效操作。支持常见的树操作如遍历、插入、删除、节点移动、浅/深复制、子树切割等。支持用户定义的数据负载以加速您的模型构建。漂亮的树显示和文本/json 转储用于漂亮的显示和离线分析。与 Python 2 和 3 兼容
Snyk.io是一家专注于帮助企业解决开源软件安全问题的公司给出评价是83分。
1. treelib安装
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple treelib github地址https://github.com/caesar0301/treelib 2. 树结构应用需求
如图所示一个分层次计算因素得分例如“流动客户”得分是由其子节点因素“货运客户”与“旅游客户”合计得到计算公式为货运客户的权重×货运客户的评价值旅游客户的权重×旅游客户的评价值。 通用计算公式如下 y ∑ i 0 n w i x i y\sum_{i0}^{n}{w_i x_i} y∑i0nwixi 其中 w i w_i wi为任意因素节点的权重 x i x_i xi为任意因素节点的评价得分值 y y y是这些节点的父节点。
遍历整个树计算最后的得分采用递归方案程序框图如下
3. 入门使用
3.1. 创建一棵树
treelib 树由Tree和Node两个类完成其中Node是树中的节点主要由如下内容
identifier唯一标识tag标签data数据
其他自行看源代码包括父、子节点关系等内容。
在实际应用中本文对“data”扩展使用元组来定义更多的数据010是评价得分值1是权重。
from treelib import Node, Tree
tree Tree()
tree.create_node(Harry, harry,data(None,None)) # root node
tree.create_node(Jane, jane, parentharry,data(None,0.6))
tree.create_node(Bill, bill, parentharry,data(None,0.4))
tree.create_node(Diane, diane, parentjane,data(None,0.3))
tree.create_node(Mary, mary, parentjane,data(None,0.35))
tree.create_node(Mark, mark, parentjane,data(None,0.25))
tree.create_node(Green, green, parentbill,data(None,0.3))
tree.create_node(White, white, parentbill,data(None,0.7))3.2. 树的简单操作
获取所有的叶子节点。
leaves tree.leaves()
leaves[Node(tagDiane, identifierdiane, data(None, 0.3)),Node(tagMary, identifiermary, data(None, 0.35)),Node(tagMark, identifiermark, data(None, 0.25)),Node(tagGreen, identifiergreen, data(None, 0.3)),Node(tagWhite, identifierwhite, data(None, 0.7))]给叶子节点赋值易便后续进行计算。
factors_data{Green:20,Mark:30,Mary:100,Diane:50,White:40}
for node in leaves:node.data(factors_data[node.tag],node.data[1])
leaves[Node(tagDiane, identifierdiane, data(50, 0.3)),Node(tagMary, identifiermary, data(100, 0.35)),Node(tagMark, identifiermark, data(30, 0.25)),Node(tagGreen, identifiergreen, data(20, 0.3)),Node(tagWhite, identifierwhite, data(40, 0.7))]获取兄弟节点。
# Return the siblings of given nid. 获取兄弟节点
tree.siblings(diane)[Node(tagMary, identifiermary, data(100, 0.35)),Node(tagMark, identifiermark, data(30, 0.25))]获取父节点。
#Get parent :class:Node object of given id.
tree.parent(diane)Node(tagJane, identifierjane, data(None, 0.6))4. 实际应用
按权重和评价的分值计算整颗树的各个因素的得分值。
# 计算综合评价
# 输入任意个节点因素,endnid是约定结束节点
def calscore(tree, firstnode, endnid): nid firstnode.identifier# 处理根节点 if (tree.parent(nid) None or firstnode.identifier endnid ) and firstnode.data[0]!None:#print(root end)return firstnodeelif tree.parent(nid) None:parentnode firstnodeelse:parentnode tree.parent(nid)if firstnode.data[0]None:# 没有计算直接取子节点childnodes tree.children(nid)# 计算分值calscore(tree, childnodes[0], endnid)else:# 已经计算找兄弟节点必须有兄弟否则合并节点siblings tree.siblings(nid)for node in siblings: if node.data[0]None:# 没有计算直接取子节点childnodes tree.children(node.identifier)# 计算分值calscore(tree, childnodes[0], endnid)# 兄弟节点都已经计算有数据的情况计算父节点得分siblings.append(firstnode)score 0for node in siblings:score score node.data[0]*node.data[1]parentnode.data(score,parentnode.data[1]) print(parentnode.tag ,parentnode.data)calscore(tree, parentnode, endnid)nid white # harry
#nid janefirstnode tree.get_node(nid)calscore(tree, firstnode, harry)
# 遍历树
print(,.join([tree[node].tag str(tree[node].data) for node in tree.expand_tree(modeTree.DEPTH)]))Harry(48.1, None),Bill(34.0, 0.4),Green(20, 0.3),White(40, 0.7),Jane(57.5, 0.6),Diane(50, 0.3),Mark(30, 0.25),Mary(100, 0.35)5. 锦上添花画棵树
绘图使用graphvizGraphviz 输入是一个用 dot 语言编写的绘图脚本通过对输入脚本的解析分析出其中的点、边及子图然后根据属性进行绘制。
关于graphviz的使用参见Python安装使用graphviz经验Format: “png“ not recognized。
# Generate DOT code file
tree.to_graphviz(hello.dot)# Can run the following command directly from the terminal as well.
import subprocess
subprocess.call([dot, -Tpng, hello.dot, -o, graph1.png])关于subprocess 运行python的时候我们都是在创建并运行一个进程。像Linux进程那样一个进程可以fork一个子进程并让这个子进程exec另外一个程序。在Python中我们通过标准库中的subprocess包来fork一个子进程并运行一个外部的程序。 subprocess包中定义有数个创建子进程的函数这些函数分别以不同的方式创建子进程所以我们可以根据需要来从中选取一个使用。另外subprocess还提供了一些管理标准流(standard stream)和管道(pipe)的工具从而在进程间使用文本通信。 此图dot描述为
digraph tree {harry [labelHarry, shapecircle]bill [labelBill, shapecircle]jane [labelJane, shapecircle]green [labelGreen, shapecircle]white [labelWhite, shapecircle]diane [labelDiane, shapecircle]mark [labelMark, shapecircle]mary [labelMary, shapecircle]harry - janeharry - billbill - greenbill - whitejane - dianejane - maryjane - mark
}6. 其他树解决方案参考
使用内置的defaultdict 我们可以很容易的定义一个树形数据结构。例如参考博文【一行python实现树形结构的方法】。
def tree(): return defaultdict(tree)users tree()
users[harold][username] bell
users[handler][username] master我们可以使用print(json.dumps(users))以json的形式输出于是我们看到 {harold: {username: bell}, handler: {username: master}}参考
https://treelib.readthedocs.io/en/latest/ XerCis. Python树结构库treelib. CSDN博客. 2022.04 mowangdk. 一行python实现树形结构的方法 . 脚本之家. 2019.08 肖永威. Python安装使用graphviz经验Format: “png“ not recognized. CSDN博客. 2023.10