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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:25:18+00:00 2026-06-18T10:25:18+00:00

The second recursion question. I need to assign unique ID’s to values in a

  • 0

The second recursion question. I need to assign unique ID’s to values in a recursive function.

Think of a folder structure, where each folder can have items or other folders. But each of those need a unique ‘id’.

I figured, since I can’t use any global variables in my recursive functions in python, that I’d increment the id with each call. But I couldn’t be more wrong. Think of the following situation.

1 – Folder

2- Folder

3 – Item

4 – Item

2 – Folder

Because of the way the recursion works, the id gets assigned twice, and there’d be no way to check. How would I go and do this? (I’m using python. Whenever I have a variable ‘uniqueid’ that I increment I get the error:

local variable ‘uniqueid’ referenced before assignment

)
My code: make_link_item just returns a string.

def build_xml(node,depth,itemid):
    #if 'Children' in node:
        #print colleges
    depth += 1
    for child in node['Children']:
        itemid += 1
        print ("  " * (depth - 1)) + "<item identifier=\"%s\" identifierref=\"%s\">" % (("itm" + ("%05d" % itemid)),("res" + ("%05d" % itemid)))
        print ("  " * depth) + "<title>%s</title>" % child['Name'].encode('utf-8')
        build_xml(child,depth,itemid)
        print ("  " * (depth - 1)) + "</item>"
    lectures = getlectures(node['Id'])
    if lectures:
        build_lectures(lectures,itemid,itemid)

def build_lectures(lectures,itemid,parentid):
    for x in range(len(lectures)):
        itemid += 1
        if x % 2 == 0:
            print "<item identifier=\"%s\" identifierref=\"%s\">" % (("itm" + ("%05d" % itemid)),("res" + ("%05d" % itemid)))
            print "<title>" + lectures[x].encode('utf-8') + "</title>"
            print "</item>"
        else:
            make_link_item(lectures[x-1].encode('utf-8'),lectures[x].encode('utf-8'),itemid,parentid)

Thanks,

Mats

  • 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-06-18T10:25:19+00:00Added an answer on June 18, 2026 at 10:25 am

    I hope I don’t miss the question.

    You could go with a class Builder that is the object for building xml arborescence. Your class has got a member increment, which you increment by one each time you meet a new item. This way, there are no two same IDs.

    class Builder:
    
       def __init__(self):
            self.increment = 0
    
       #other methods you need
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Second question of the day but hopefully simple, I just need to change the
Warning, this is a bit recursive ;) I answered this question: Python:How can i
(second question today - must be a bad day) I have a dataframe with
the second function gives error C2803 http://msdn.microsoft.com/en-us/library/zy7kx46x%28VS.80%29.aspx : 'operator ,' must have at least
the second function isnt working? $('.edit_hover').live('hover', function(e){ $(this).stop(); var half_width = ($(this).css('width').slice(0, -2))/2; var
Referring second answer to question: How to convert from ASCII to Hex and vice
What's the second minimum value that a decimal can represent? That is the value
This question is more a semantic-algorithmic-data-structure question than a F# syntactically question. I have
Another recursion question. What I've tried to develop for the past few hours is
Following up from my previous question. Can anyone explain why the following code compiles

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.