Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 844621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:18:52+00:00 2026-05-15T06:18:52+00:00

Suppose I have a bulleted list like this: * list item 1 * list

  • 0

Suppose I have a bulleted list like this:

* list item 1
* list item 2 (a parent)
** list item 3 (a child of list item 2)
** list item 4 (a child of list item 2 as well)
*** list item 5 (a child of list item 4 and a grand-child of list item 2)
* list item 6

I’d like to parse that into a nested list or some other data structure which makes the parent-child relationship between elements explicit (rather than depending on their contents and relative position). For example, here’s a list of tuples containing an item and a list of its children (and so forth):

Edit: Hopefully, a more correct list example, where each element in the list is a tuple containing: a bullet’s text and, if applicable, a list of children (in the same form).

    [('list item 1',),
     ('list item 2', [('list item 3',), ('list item 4', [('list item 5',)])]
     ('list item 6',)]

[('list item 1',),
 ('list item 2', [('list item 3',), ('list item 4', [('list item 5',)])]),
 ('list item 6',)]

I’ve attempted to do this with plain Python and some experimentation with Pyparsing, but I’m not making progress. I’m left with two major questions:

  1. What’s the strategy I need to employ to make this work? I know recursion is part of the solution, but I’m having a hard time making the connection between this and, say, a Fibonacci sequence.
  2. I’m certain I’m not the first person to have done this, but I don’t know the terminology of the problem to make fruitful searches for more information on this topic. What problems are related to this so that I can learn more about solving these kinds of problems in general?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T06:18:53+00:00Added an answer on May 15, 2026 at 6:18 am

    I can’t parse your desired result — it seems to have more open parentheses than corresponding closed ones and I don’t understand the logic behind it.

    To make a tree structure explicit, what about, e.g.:

    data = '''* list item 1
    * list item 2
    ** list item 3
    ** list item 4
    *** list item 5
    * list item 6'''.splitlines()
    
    class Node(object):
      def __init__(self, payload):
        self.payload = payload
        self.children = []
      def show(self, indent):
        print ' '*indent, self.payload
        for c in self.children:
          c.show(indent+2)
    
    def makenest(linelist):
      rootnode = Node(None)
      stack = [(rootnode, 0)]
      for line in linelist:
        for i, c in enumerate(line):
          if c != '*': break
        stars, payload = line[:i], line[i:].strip()
        curlev = len(stars)
        curnod = Node(payload)
        while True:
          parent, level = stack[-1]
          if curlev > level: break
          del stack[-1]
        # a child node of the current top-of-stack
        parent.children.append(curnod)
        stack.append((curnod, curlev))
      rootnode.show(0)
    
    makenest(data)
    

    The show method of course exists just for the purpose of verifying that the part about parsing the strings and creating the tree has worked correctly. If you can specify more precisely exactly how it is that you want to transform your tree into nested tuples and lists, I’m sure it will be easy to add to class Node the appropriate (and probably recursive) method — so, could you please give this missing specification…?

    Edit: since the OP has clarified now, it does, as predicted, become easy to satisfy the spec. Just add to class Node the following method:

      def emit(self):
        if self.children:
          return (self.payload,
                  [c.emit() for c in self.children])
        else:
          return (self.payload,)
    

    and change the last three lines of the code (last one of makenest, a blank one, and the module-level call to makenest) to:

      return [c.emit() for c in rootnode.children]
    
    print(makenest(data))
    

    (The parentheses after print are redundant but innocuous in Python 2, required in Python 3, so I put them there just in case;-).

    With these tiny changes, my code runs as requested, now emitting

    [('list item 1',), ('list item 2', [('list item 3',), ('list item 4', [('list item 5',)])]), ('list item 6',)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose I have a table called Companies that has a DepartmentID column. There's also
Can anyone summarize or bulleted list the types of things that go in each
Suppose I have a list of sets and I want to get the union
Suppose I have a doubly linked list. I create it as such: MyList list
Suppose I have a table like: id............value ``````````````````` A............1 A............2 A............3 B............1 and so
Suppose I have logged into an application which is running from IIS . Now
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose we have a table A: itemid mark 1 5 2 3 and table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.