How to convert a navigation list with depth levels to a parent-child flat list?

How to convert a navigation list with depth levels to a parent-child flat list?

Content Index :

How to convert a navigation list with depth levels to a parent-child flat list?
Tag : python , By : wraith
Date : January 11 2021, 03:32 PM

this one helps. I have these lists in python:
docs_id = ['10','20','21','22','30','31','32','40','41','42','421','422']

text = ['1. Chapter', '2. Chapter', '2.1 Chapter',
        '2.2 Chapter','3. Chapter', '3.1 Chapter','3.2 Chapter',
        '4. Chapter','4.1. Chapter','4.2 Chapter',
        '4.2.1 Chapter','4.2.2 Chapter']

depth= [1, 1, 2, 2, 1, 2, 2, 1, 2, 2, 3, 3]

currentdepth = 1

tree = []

current_doc_id = 0

for i, value in enumerate(text):

    newdepth = depth[i]
    doc_id = docs_id[i]
    title = value

    if newdepth > currentdepth:

        global current_doc_id
        current_doc_id =  docs_id[i-1]
        tree.append([doc_id, title, int(current_doc_id)])

        currentdepth += 1
    elif newdepth == currentdepth:
        tree.append([doc_id, title, int(current_doc_id)])
        tree.append([doc_id, title, int(current_doc_id)])
        while newdepth>currentdepth:
            currentdepth -=1

for i in tree:

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Entity Framework 6: Adding child object to parent's list vs. setting child's navigation property to parent

Tag : chash , By : user180941
Date : March 29 2020, 07:55 AM
Hope this helps Yes, the two approaches are interchangeable. This allows you to create and save your object graph to the database from either the perspective of the MailServer or the MailDomain.
If you do code-first, you have the option of removing the properties and mappings if they're not needed.

XML parent child structure from flat list

Tag : vba , By : Anand
Date : March 29 2020, 07:55 AM
Any of those help Something like this might work for you. Has no error checking on the levels (will break if there's a "level 4" with no previous "level 3").
Sub Tester()

    Dim d, doc, root, lvl As Long, r, el, id
    Dim parents(0 To 20)  'handle up to 20 levels...

    Set doc = New MSXML2.DOMDocument
    Set root = doc.createElement("root")
    doc.appendChild root

    Set parents(0) = root 'Parent node for all "Level 1" nodes...

    d = Range("a1").CurrentRegion.Value

    For r = LBound(d, 1) To UBound(d, 1)

        lvl = CLng(Split(d(r, 1), " ")(1)) 'get level

        Set el = doc.createElement("section")
        el.setAttribute "ident", d(r, 2)

        parents(lvl - 1).appendChild el
        Set parents(lvl) = el ' Make this the current Parent node for
                              '   any nodes directly below

    Next r

    Debug.Print PrettyPrintXML(doc.XML)

End Sub

React Native navigation for child components in flat list

Tag : javascript , By : Mena
Date : March 29 2020, 07:55 AM
wish of those help Im pretty new in React Native (and Javscript) and im trying to understand react native navigation. , You can do it as following.
    render() {
        return (
        //    <List>
                    renderItem={({ item }) => (




Need to convert list of data into list of list based on parent child relationship

Tag : java , By : Adam
Date : March 29 2020, 07:55 AM
it should still fix some issue For representing the tree structure, I have used an ArrayList, where the index of the node is equal to its index in the array + 1. If you have a sparse tree/some indices might be missing, use an equivalent approach with a map instead.
Solution using Java 8 stream API:
public static void main( String[] args ) {
        List<ResponseData> responseDataList = Arrays.asList(
            new ResponseData( 1, -1 ),  // changed null to -1 as null can't be a map key
            new ResponseData( 2, 1 ),
            new ResponseData( 3, 1 ),
            new ResponseData( 4, 1 ),
            new ResponseData( 5, 2 ),
            new ResponseData( 6, 2 ),
            new ResponseData( 7, 3 ),
            new ResponseData( 8, 3 ),
            new ResponseData( 9, 4 ),
            new ResponseData( 10, 4 ),
            new ResponseData( 11, 5 ),
            new ResponseData( 12, -1 ),
            new ResponseData( 13, 12 )
        final Map<Integer, List<ResponseData>> map = responseDataList.stream()
                .collect( Collectors.groupingBy( o -> getLevel( responseDataList, o, 0 ) ) );
        System.out.println( map );
        // To convert the Map to a List of Lists:
        System.out.println( new ArrayList<>( map.values() ));

    private static int getLevel(List<ResponseData> nodes, ResponseData responseData, int level) {
        if( responseData.parent == -1 ) {
            return level;
        } else {
            return getLevel( nodes, nodes.get( responseData.parent - 1 ), level + 1 );  // -1 to adjust index

    private static final class ResponseData {
        public int id;
        public int parent;

        public ResponseData( int id, int parent ) {
            this.id = id;
            this.parent = parent;

        public String toString() {
            return String.format( "{id: %d, parent: %d}", id, parent );

convert a flat list into a tree with limited number of nodes per depth

Tag : java , By : Nick Pegg
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • invalid literal for int() with base 10: - django
  • Why does my code print a value that I have not assigned as yet?
  • the collatz func in automate boring stuff with python
  • How to find all possible combinations of parameters and funtions
  • about backpropagation deep neural network in tensorflow
  • Sort strings in pandas
  • How do access my flask app hosted in docker?
  • Replace the sentence include some text with Python regex
  • Counting the most common element in a 2D List in Python
  • logout a user from the system using a function in python
  • mp4 metadata not found but exists
  • Django: QuerySet with ExpressionWrapper
  • Pandas string search in list of dicts
  • Decryption from RSA encrypted string from sqlite is not the same
  • need of maximum value in int
  • a list of several tuples, how to extract the same of the first two elements in the small tuple in the large tuple
  • Display image of 2D Sinewaves in 3D
  • how to prevent a for loop from overwriting a dictionary?
  • How To Fix: RuntimeError: size mismatch in pyTorch
  • Concatenating two Pandas DataFrames while maintaining index order
  • Why does this not run into an infinite loop?
  • Python Multithreading no current event loop
  • Element Tree - Seaching for specific element value without looping
  • Ignore Nulls in pandas map dictionary
  • How do I get scrap data from web pages using beautifulsoup in python
  • Variable used, golobal or local?
  • I have a regex statement to pull all numbers out of a text file, but it only finds 77 out of the 81 numbers in the file
  • How do I create a dataframe of jobs and companies that includes hyperlinks?
  • Detect if user has clicked the 'maximized' button
  • Does flask_login automatically set the "next" argument?
  • Indents in python 3
  • How to create a pool of threads
  • Pandas giving IndexError on one dataframe but not on another similar dataframe
  • Django Rest Framework - Testing client.login doesn't login user, ret anonymous user
  • Running dag without dag file in airflow
  • Filling across a specified dimension of a numpy array
  • Python populating dataframe in pandas from text files
  • How to interpolate a single ("non-piecewise") cubic spline from a set of data points?
  • Divide 2 integers (leetcode 29) - recursion issue
  • Can someone explain why do I get this output in Python?
  • How do I scrape pdf and html from search results without obvious url
  • Is there a way to automatically make a "collage" of plots with matplotlib?
  • How to combine multiple rows in pandas with shared column values
  • How do I get LOAD_CLASSDEREF instruction after dis.dis?
  • Django - How to add items to Bootstrap dropdown?
  • Linear Regression - Does the below implementation of ridge regression finding coefficient term using gradient method is
  • How to drop all rows in pandas dataframe with negative values?
  • Most Efficient Way to Find Closest Date Between 2 Dataframes
  • Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments
  • Looping through a function
  • Create a plot for each unique ID
  • a thread python with 'while' got another thread never start
  • Solution from SciPy solve_ivp contains oscillations for a system of first-order ODEs
  • trigger python events driven by selenium controlled browser
  • Passing line-edits to a contextmanager to set validators
  • Python: globals().items() iterations try to change a dict
  • Is it possible to specify starting values for each parameter (instead of bounds) for scipy's differential evolution?
  • why datetime.now() and constructed datetime using all fields(like year,month...) of now has big timedelta?
  • MySQL multiple table UPDATE query using sqlalchemy core?
  • find if a semantic version is superset of of another version python
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com