Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting data hierarchically
    primarykey
    data
    text
    <p>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 </p> <pre><code>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} } </code></pre> <p>In each <em>sub</em>-list, the data is structured this way <code>{*article_id*, *parent_id*}</code> If the article_id and parent_id are the same it obviously mean that article has no parent.</p> <p>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:</p> <pre><code> 1 -45 --566 ---783 -66 --911 </code></pre> <p>.... for article id 1</p> <p>I could only sort out the highest level (Ist and 2nd generation) ids. Having problem getting the 3rd and subsequent generations.</p> <p>This is the code I used:</p> <pre><code>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) </code></pre> <p>My code sadly does not work.</p> <p>Any help/nudge in the right direction will be appreciated. Thanks</p> <p>David</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload