有没有办法从 pandas DataFrame 读取数据并使用 anytree 构造树?
\nParent Child\nA A1\nA A2\nA2 A21\nRun Code Online (Sandbox Code Playgroud)\n我可以使用静态值来做到这一点,如下所示。但是,我想通过使用 anytree 从 pandas DataFrame 读取数据来自动执行此操作。
\n>>> from anytree import Node, RenderTree\n>>> A = Node("A")\n>>> A1 = Node("A1", parent=A)\n>>> A2 = Node("A2", parent=A)\n>>> A21 = Node("A21", parent=A2)\nRun Code Online (Sandbox Code Playgroud)\n输出是
\nA\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 A1\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 A2\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 A21\nRun Code Online (Sandbox Code Playgroud)\n这个问题,特别是答案已经被采纳,实际上是复制的,来自:
\n\n非常感谢@Fabien N
\n我正在按照anytree 包的这个文档来渲染图形。请注意我已经在我的 mac 上安装了 graphviz 使用
pip3 install graphviz
Run Code Online (Sandbox Code Playgroud)
当我尝试执行此操作时
RenderTreeGraph(udo).to_picture("udo.png")
Run Code Online (Sandbox Code Playgroud)
我收到以下错误
回溯(最近一次调用):文件“”,第 1 行,在 RenderTreeGraph(udo).to_picture("udo.png") 文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ site-packages/anytree/dotexport.py”,第 51 行,在 to_picture check_call(cmd) 文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py”,第 286 行,在check_call retcode = call(*popenargs, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call with Popen(*popenargs, * *kwargs) 作为 p:文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py”,第 707 行,在init 中 restore_signals, start_new_session) 文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py”,第 1326 行,在 _execute_child 中 raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] 没有这样文件或目录:'点'
请帮我渲染树
社区。我需要接受多个以逗号分隔的输入来生成信息摘要(具体来说,每个小组/项目中有多少不同的员工参与)?该程序以字符串的形式获取员工、经理和团体。
\n我正在使用anytree python 库来搜索/计算每个组中每个员工的出现次数。但是,该程序一次仅接受一个值/单元格,而不是多个值。
这是树结构以及我如何接受输入值?
\nPress q to exit, Enter your data: Joe\nPress q to exit, Enter your data: Manager1\nPress q to exit, Enter your data: Group1\nPress q to exit, Enter your data: Charles \nPress q to exit, Enter your data: Manager1\nPress q to exit, Enter your data: Group2\nPress q to exit, Enter your data: Joe\nPress q to exit, Enter your data: Manager3\nPress q to exit, Enter your data: Group1\nPress q to exit, Enter your data: …Run Code Online (Sandbox Code Playgroud) 有没有办法从文件中读取数据并使用 anytree 构造树?
Parent Child
A A1
A A2
A2 A21
Run Code Online (Sandbox Code Playgroud)
我可以使用静态值来做到这一点,如下所示。但是,我想通过使用 anytree 从文件中读取数据来自动执行此操作。
>>> from anytree import Node, RenderTree
>>> A = Node("A")
>>> A1 = Node("A1", parent=A)
>>> A2 = Node("A2", parent=A)
>>> A21 = Node("A21", parent=A2)
Run Code Online (Sandbox Code Playgroud)
输出是
A
??? A1
??? A2
??? A21
Run Code Online (Sandbox Code Playgroud) 使用anytree我生成了这样的树:
A\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 B\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 C\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 D\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 F\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 B\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 C\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 E\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 G\nRun Code Online (Sandbox Code Playgroud)\n\n有没有办法删除所有重复的子级并将其变成下面的树(对所有可能级别的子级进行递归)?
\n\nA\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 B\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 C\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 D\n | \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 F\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 E\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 G\nRun Code Online (Sandbox Code Playgroud)\n\n编辑:
\n\n我想要实现的是网站上所有链接的树。因此斜杠之间的所有内容都将成为子项:(.../child/...第二个斜杠是可选的)。以上只是我的问题的表述,但我希望它是清楚的。
这是我的节点生成:
\n\nroot = Node(\'A\')\nfor link in links:\n children = link.split(\'/\')\n cur_root = Node(children[0], parent=root)\n for one in children[1:]:\n cur_root = Node(one, parent=cur_root)\nRun Code Online (Sandbox Code Playgroud)\n 我正在["abc", "abd", "aec", "add", "adcf"]使用任何python3包创建一个列表中的树.在这个树中,每个列表元素的第一个字符 - a是一个根,随后,其他字符被添加为它们的子元素.当我渲染树时,它看起来像:
to_picture
但是当我使用["abc", "abd", "aec", "add", "adcf"]方法将树渲染到图片时,图像是 -
我不希望合并公共节点,因为它向我的树添加了不需要的路径.
提前致谢.
我当前正在使用anytree来生成搜索树,如何以列表格式获取从根节点开始的所有可能的分支
from anytree import Node, RenderTree, AsciiStyle
f = Node("f")
b = Node("b", parent=f)
a = Node("a", parent=b)
d = Node("d", parent=b)
c = Node("c", parent=d)
e = Node("e", parent=d)
g = Node("g", parent=f)
i = Node("i", parent=g)
h = Node("h", parent=i)
print(RenderTree(f, style=AsciiStyle()).by_attr())
Run Code Online (Sandbox Code Playgroud)
当前树:
f
|-- b
| |-- a
| +-- d
| |-- c
| +-- e
+-- g
+-- i
+-- h
Run Code Online (Sandbox Code Playgroud)
想要的输出(treeBranch):
[[f、b、a]、[f、b、d、c]、[f、b、d、e]、[f、g、i、h]]
我不确定是否有更好的方法来做到这一点,欢迎任何建议。
我想使用此列表来检查树中是否存在来自用户的新路径,例如:
newPath = [f, b]
for branch in treeBranch: …Run Code Online (Sandbox Code Playgroud) anytree ×7
python ×5
python-3.x ×4
tree ×3
graphviz ×2
list ×2
duplicates ×1
file ×1
pandas ×1