[10] Python learning notes: simple multi-level directory (Dictionary nesting)

Simple multi-level directory (Dictionary nesting)

# Create a multi-level directory with the functions of adding nodes, viewing nodes, and returning to the previous level
db = {
    'Beijing' : {},
    'Shanghai' : {},
    'Guangzhou' : {
        'Tianhe District' : {},
        'Liwan District' : {},
        'Baiyun District' : {
            'White Cloud Mountain' : {}
        }
    }
}
path = []

while True:
    temp = db
    for item in path:
        temp = temp[item]
    print('All currently optional nodes:',list(temp.keys()),'\n')

    choice = input("1: Add node; 2: View node(Q Sign out/B Go back to the previous level)\n>>>")
    if choice == '1':
        k = input('Enter the name of the child node you want to add:')
        if k in temp:
            print('Child node already exists')
        else:
            temp[k] = {}
    elif choice == '2':
        k = input('Please enter the child nodes to view:')
        if k in temp:
            path.append(k)
        else:
            print('Wrong child node name')
    elif choice.lower() == 'q':
        break
    elif choice.lower() == 'b':
        if path:
            path.pop()
    else:
        print('Illegal input, please re-enter\n>>>')
The idea is as follows:
1. Create a multi-level Dictionary (for testing and validation)
2. The directory has the functions of adding nodes, viewing nodes, and returning to the previous level. Therefore, the variable choice is used to record operations
 3. In order to switch between all levels of the dictionary, create a list path to record the path. The value of each element of path is the key corresponding to all levels of the dictionary. The length of path is the depth of the current node of the dictionary
 4. When viewing a node, add the name of the entered child node to the path list, and use the elements in the path list as the index of the dictionary.
        k = input('Please enter the child nodes to view:')
        if k in temp:
            path.append(k)
        else:
            print('Wrong child node name')

Because the depth of the current node can be greater than 1, a new variable, temp, needs to be introduced for iterative operation (for is used to repeatedly index all elements in the path list,
Namely
    temp = db
    for item in path:
        temp = temp[item]
    print('All currently optional nodes:',list(temp.keys()),'\n')

5. When adding nodes, you can easily use the temp variable to operate
    choice = input("1: Add node; 2: View node(Q Sign out/B Go back to the previous level)\n>>>")
    if choice == '1':
        k = input('Enter the name of the child node you want to add:')
        if k in temp:
            print('Child node already exists')
        else:
            temp[k] = {}

6. When returning to the previous level, since the path list is used to record the path, delete the last element of the path list, and then print again
    elif choice.lower() == 'b':
        if path:
            path.pop()

7. When exiting, it is equivalent to executing break to jump out of the while loop.

Posted on Thu, 09 Jan 2020 09:33:25 -0800 by Dvector