advTee的每一个列添加不同的节点
时间: 2024-04-30 08:23:20 浏览: 76
对于advTee中的每一列,可以使用append方法添加不同的节点。例如:
```python
from anytree import Node, RenderTree, LevelOrderGroupIter, PreOrderIter
from anytree.exporter import DotExporter
from anytree.search import findall
# 创建根节点
root = Node("Root")
# 创建advTee节点
advTee = Node("advTee", parent=root)
# 为advTee添加三个子节点
col1 = Node("col1", parent=advTee)
col2 = Node("col2", parent=advTee)
col3 = Node("col3", parent=advTee)
# 为col1、col2、col3分别添加不同的子节点
Node("node1", parent=col1)
Node("node2", parent=col1)
Node("node3", parent=col2)
Node("node4", parent=col2)
Node("node5", parent=col3)
Node("node6", parent=col3)
# 打印树结构
for pre, fill, node in RenderTree(root):
print("%s%s" % (pre, node.name))
# 导出为图像
DotExporter(root).to_picture("tree.png")
# 遍历advTee的子节点
for col in findall(root, filter_=lambda node: node.name=="advTee")[0].children:
print("Column:", col.name)
for node in PreOrderIter(col):
print(node.name)
```
执行上述代码,会输出如下的树结构和节点内容:
```
Root
└── advTee
├── col1
│ ├── node1
│ └── node2
├── col2
│ ├── node3
│ └── node4
└── col3
├── node5
└── node6
Column: col1
col1
node1
node2
Column: col2
col2
node3
node4
Column: col3
col3
node5
node6
```
可以看到,advTee的每一列都有不同的子节点,并且可以通过遍历的方式依次访问每个节点。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)