【发布时间】:2021-09-30 05:08:39
【问题描述】:
我正在对 XML 文件执行广度优先搜索 (BFS) 遍历。 https://lxml.de/3.3/api.html#lxml-etre 中显示了深度优先搜索算法。但是,我需要帮助来应用基于此代码的 BFS 搜索。 以下是文档中给出的代码:
>>> root = etree.XML('<root><a><b/><c/></a><d><e/></d></root>')
>>> print(etree.tostring(root, pretty_print=True, encoding='unicode'))
<root>
<a>
<b/>
<c/>
</a>
<d>
<e/>
</d>
</root>
>>> queue = deque([root])
>>> while queue:
... el = queue.popleft() # pop next element
... queue.extend(el) # append its children
... print(el.tag)
根 一种 d b C e
我需要帮助来尝试附加它以使其适合 BFS 遍历。下面是我尝试编写的代码示例,但它无法正常工作。有人可以帮忙吗。 我的代码: 从集合导入双端队列
>>> d = deque([root])
>>> while d:
>>> el = d.pop()
>>> d.extend(el)
>>> print(el.tag)
谢谢
【问题讨论】:
标签: python xml xml-parsing lxml breadth-first-search