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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:41:51+00:00 2026-06-16T22:41:51+00:00

My python program returns a list containing data of sub-list. Each sub-list contains the

  • 0

My python program returns a list containing data of sub-list. Each sub-list contains the unique id of an article and the parent id of that article viz

pages_id_list ={ {22, 4},{45,1},{1,1}, {4,4},{566,45},{7,7},{783,566}, {66,1},{300,8},{8,4},{101,7},{80,22}, {17,17},{911,66} }

In each sub-list, the data is structured this way {*article_id*, *parent_id*}
If the article_id and parent_id are the same it obviously mean that article has no parent.

I would like to sort the data using minimal code such that for each article, I can readily
access a list of it’s children and grandchildren (nested data) if available. For example (using the example data above) I should be able to print at the end of the day:

 1
 -45
 --566
 ---783
 -66
 --911

…. for article id 1

I could only sort out the highest level (Ist and 2nd generation) ids. Having problem getting the 3rd and subsequent generations.

This is the code I used:

highest_level = set()
first_level = set()
sub_level = set()

for i in pages_id_list:
    id,pid = i['id'],i['pid']

    if id == pid:
        #Pages of the highest hierarchy
        highest_level.add(id)

for i in pages_id_list:
    id,pid = i['id'],i['pid']

    if id != pid :
        if pid in highest_level:
            #First child pages
            first_level.add(id)
        else:
            sub_level.add(id)

My code sadly does not work.

Any help/nudge in the right direction will be appreciated.
Thanks

David

  • 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-16T22:41:52+00:00Added an answer on June 16, 2026 at 10:41 pm

    Maybe something like this:

    #! /usr/bin/python3.2
    
    pages_id_list = [ (22, 4),(45,1),(1,1), (4,4),(566,45),(7,7),(783,566), (66,1),(300,8),(8,4),(101,7),(80,22), (17,17),(911,66) ]
    
    class Node:
        def __init__ (self, article):
            self.article = article
            self.children = []
            self.parent = None
    
        def print (self, level = 0):
            print ('{}{}'.format ('\t' * level, self.article) )
            for child in self.children: child.print (level + 1)
    
    class Tree:
        def __init__ (self): self.nodes = {}
    
        def push (self, item):
            article, parent = item
            if parent not in self.nodes: self.nodes [parent] = Node (parent)
            if article not in self.nodes: self.nodes [article] = Node (article)
            if parent == article: return
            self.nodes [article].parent = self.nodes [parent]
            self.nodes [parent].children.append (self.nodes [article] )
    
        @property
        def roots (self): return (x for x in self.nodes.values () if not x.parent)
    
    t = Tree ()
    for i in pages_id_list: t.push (i)
    for node in t.roots: node.print ()
    

    This creates a tree structure which you can traverse in order to get all subitems. You can access any article via t.nodes [article] and get its children via t.nodes [article].children.

    The output of the print method is:

    1
        45
            566
                783
        66
            911
    4
        22
            80
        8
            300
    7
        101
    17
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how do i code a python program that return a json element that look
I'm writing a python program that uses scons to build an .exe and then
I have a Python program that executes a large MySQL statement. How do I
I have a python program that takes the md5 & sha1 hash values of
I have a python program that must work on Windows and Linux. There are
I have a simple python program that I'd like to daemonize. Since the point
There are many ways to write a Python program that computes a histogram. By
I am attempting to run a python program that can run a dictionary from
I am attempting to run a python program that can run a dictionary from
I'm writing a program to read in data from a list, Fourier transform and

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.