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
whileloop?