嵌套
  • 使用递归 Python 函数的导航菜单

发布于 2024-09-02 08:19:14 字数 689 浏览 3 评论 0原文

我想将此数据结构呈现为无序列表。

menu = [
         [1, 0],
           [2, 1],
           [3, 1],
             [4, 3],
             [5, 3],
               [6, 5],
         [7,1]
        ]

[n][0] 是关键
[n][1] 引用父键

所需的输出是:

<ul>
<li>Node 1</li>

  <ul>
  <li>Node 2</li>
  <li>Node 3</li>

    <ul>
    <li>Node 4</li>
    <li>Node 5</li>

      <ul>
      <li>Node 6</li>
      </ul>

    </ul>

   <li>Node 7</li>
   </ul>

</ul>

我可能可以在不使用递归的情况下完成此操作,但这并不有趣。用递归解决这个问题最有效的方法是什么?

谢谢!

I want to render this data structure as an unordered list.

menu = [
         [1, 0],
           [2, 1],
           [3, 1],
             [4, 3],
             [5, 3],
               [6, 5],
         [7,1]
        ]

[n][0] is the key
[n][1] references the parent key

The desired output is:

<ul>
<li>Node 1</li>

  <ul>
  <li>Node 2</li>
  <li>Node 3</li>

    <ul>
    <li>Node 4</li>
    <li>Node 5</li>

      <ul>
      <li>Node 6</li>
      </ul>

    </ul>

   <li>Node 7</li>
   </ul>

</ul>

I could probably do this without recursion but that would be no fun. What is the most efficient way to solve this problem with recursion?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-09-09 08:19:14
def render(nodes, parent = 0):
    if parent not in nodes:
        return
    print('<ul>')
    for n in nodes[parent]:
        print('<li>Node %d</li>' % n)
        render(nodes, n)
    print('</ul>')

这是输出

>>> nodes = {}
>>> for n in menu:
    if n[1] not in nodes:
        nodes[n[1]] = []
    nodes[n[1]].append(n[0])
>>> render(nodes)
<ul>
<li>Node 1</li>
<ul>
<li>Node 2</li>
<li>Node 3</li>
<ul>
<li>Node 4</li>
<li>Node 5</li>
<ul>
<li>Node 6</li>
</ul>
</ul>
<li>Node 7</li>
</ul>
</ul>
def render(nodes, parent = 0):
    if parent not in nodes:
        return
    print('<ul>')
    for n in nodes[parent]:
        print('<li>Node %d</li>' % n)
        render(nodes, n)
    print('</ul>')

Here is the output

>>> nodes = {}
>>> for n in menu:
    if n[1] not in nodes:
        nodes[n[1]] = []
    nodes[n[1]].append(n[0])
>>> render(nodes)
<ul>
<li>Node 1</li>
<ul>
<li>Node 2</li>
<li>Node 3</li>
<ul>
<li>Node 4</li>
<li>Node 5</li>
<ul>
<li>Node 6</li>
</ul>
</ul>
<li>Node 7</li>
</ul>
</ul>
赠意 2024-09-09 08:19:14

即使您的结构如此简单,我也不会使用二元素列表。使用一些TreeNode类,并给它一个适当的__str__方法,例如

class TreeNode(object):
    # ...
    # methods for adding children (instances of TreeNode again) etc.

    def __str__(self):
        ret = "<li>%s" % self.value

        if self.children:
            children = "".join([str(c) for c in self.children])
            ret += "<ul>%s</ul>" % children 
        ret += "</li>"

        return ret

......或类似的东西。不过没有测试过。将整个树的表示包含在

    标记中。

I would not use two-element lists, even if your structure is that simple. Use some TreeNode class, and give it an appropriate __str__ method, e.g.

class TreeNode(object):
    # ...
    # methods for adding children (instances of TreeNode again) etc.

    def __str__(self):
        ret = "<li>%s" % self.value

        if self.children:
            children = "".join([str(c) for c in self.children])
            ret += "<ul>%s</ul>" % children 
        ret += "</li>"

        return ret

... or something like that. Didn't test it, though. Enclose the whole tree's representation in an <ul> tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文