How do I build a tree dynamically in Python

后端 未结 3 1446
盖世英雄少女心
盖世英雄少女心 2021-01-05 19:08

A beginner Python/programming question... I\'d like to build a tree structure in Python, preferably based on dictionaries. I found code that does this neatly:



        
3条回答
  •  孤城傲影
    2021-01-05 19:44

    This

    root['toplevel']['secondlevel']['thirdlevel'] = 1
    

    can also be done like this:

    node = root
    for key in ('toplevel', 'secondlevel'):
        node = node[key]
    node['thirdlevel'] = 1
    

    I hope that gives you an idea.

提交回复
热议问题