循环遍历字典值并随后打印

nm6*_*834 2 python dictionary for-loop classification hierarchy

我试图以层次结构格式打印以下字典

fam_dict{'6081740103':['60817401030000','60817401030100','60817401030200',
'60817401030300','60817401030400','60817401030500','60817401030600'] 
Run Code Online (Sandbox Code Playgroud)

如下图所示:

60817401030000
    60817401030100
        60817401030200
            60817401030400
                60817401030500
                    60817401030600
Run Code Online (Sandbox Code Playgroud)

到目前为止,我有以下代码,但我必须手动输入每行的第i个索引.如何以递归格式重新调整此代码,而不必计算多少行代码并每次手动输入索引值

  my_p = node(fam_dict['6081740103'][0], None)
    my_c = node(fam_dict['6081740103'][1], my_p)
    my_d = node(fam_dict['6081740103'][2], my_c)
    my_e = node(fam_dict['6081740103'][4], my_d)
    my_f = node(fam_dict['6081740103'][5], my_e)
    my_g = node(fam_dict['6081740103'][6], my_f)

    print (my_p.name)
    print_children(my_p)
Run Code Online (Sandbox Code Playgroud)

Roa*_*ner 5

你可以试试这个:

fam_dict = {'6081740103':['60817401030000','60817401030100','60817401030200',
'60817401030300','60817401030400','60817401030500','60817401030600']}

for i, val in enumerate(fam_dict['6081740103']):
    print(' ' * i * 4 + val)
Run Code Online (Sandbox Code Playgroud)

哪个输出您想要的层次结构:

60817401030000
    60817401030100
        60817401030200
            60817401030300
                60817401030400
                    60817401030500
                        60817401030600
Run Code Online (Sandbox Code Playgroud)