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 6743283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:56:35+00:00 2026-05-26T11:56:35+00:00

I have read a little about mptt solution and I’m trying to implement some

  • 0

I have read a little about mptt solution and I’m trying to implement some sort of translating a nested list into a mptt list. I know, that in this format it’s useless, but nested lists can be easily changed with a classes childs or anything else.

The code I have is:

tree = [[1,[[11,[111, 112, 113]],[12,]]],[2,[[21,[[211,[2111,]],]],]]]

class node():
    def __init__(self, value, lft):
        self.value = value
        self.lft = lft

    def add_right(self, rgt):
        self.rgt = rgt

    def __str__(self):
        return "%s | %s | %s" %(self.value, self.lft, self.rgt)

def numerate(table, tree, counter):
    for item in tree:
        if type(item) == type(1):
            table.append(node(item, counter))
            index = len(table)
            counter += 1
        else:
            table.append(node(item[0], counter))
            index = len(table)
            counter += 1

            if len(item) > 1:
                (table, counter) = numerate(table, item[1], counter)

        table[index-1].add_right(counter)
    return (table, counter)

table = numerate([], tree, 0)[0]

for item in table:
    print item

But the result is:

1 | 0 | 6
11 | 1 | 5
111 | 2 | 3
112 | 3 | 4
113 | 4 | 5
12 | 5 | 6
2 | 6 | 10
21 | 7 | 10
211 | 8 | 10
2111 | 9 | 10

I see there is something not ok with the right value in recursion. I think such a format in python might be sometimes useful, so it would be nice to have some code for it already written.

  • 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-26T11:56:35+00:00Added an answer on May 26, 2026 at 11:56 am

    The problem has been solved by adding a global iterator.

    If someone is interested, that’s the new numerate function, which actually works fine.

    global_iterator = 0
    
    def numerate(table, tree):
        global global_iterator
        for item in tree:
            global_iterator += 1
    
    
            if type(item) == type(1):
                table.append(node(item, global_iterator))
                index = len(table)
            else:
                table.append(node(item[0], global_iterator))
                index = len(table)
    
                if len(item) > 1:
                    table.append(numerate(table, item[1]))
    
            global_iterator += 1
    
            table[index-1].add_right(global_iterator)
    
        return table
    
    table = numerate([], tree)
    

    The node() does not change.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that captures keystrokes. But i have read a little bit
I have read many answers to questions about dynamically resizing NSWindows and nothing has
I have read various posts in stackoverflow and I am trying to figure out
I have read few articles about table partioning but still I am bit confused
I have read some of the questions and answers here, but it none match
I'm migrating my project to JBoss AS 7, and I've read a little about
I'm trying to set up authentication on my server, however, I know little about
I've read a little about KML files. They are XML files that can be
I'm looking for a make platform. I've read a little about gnu make, and
I have read the other posts about password protecting your application, but my situation

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.