[英]How to parsing trees in Python?
我需要幫助來開發我正在研究的這個算法。 我有以下格式的樹的輸入:
Root -> AB CD
AB -> ABC CBA
CD -> CDE FGH
該算法應該讀取括號格式並給出以下 output:
Root
|
____________
AB CD
| |
__________ ___________
ABC CBA CDE FGH
也許你可以從networkx
模塊開始
安裝網絡x: pip pip install networkx
安裝matplotlib : pip install matplotlib
程序
import networkx as nx
import matplotlib.pyplot as plt
if __name__ == "__main__":
# define DiGraph
tree = nx.DiGraph()
# add node
tree.add_node("root")
tree.add_node("AB")
tree.add_node("CD")
tree.add_node("ABC")
tree.add_node("CBA")
tree.add_node("CDE")
tree.add_node("FGH")
# add connection edge
tree.add_edge("root","AB")
tree.add_edge("root","CD")
tree.add_edge("AB","ABC")
tree.add_edge("AB","CBA")
tree.add_edge("CD","CDE")
tree.add_edge("CD","FGH")
# output the tree
nx.draw(tree,with_labels = True)
plt.show()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.