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

时间:2018-06-12 04:24:04

标签: python dictionary for-loop classification hierarchy

我正在尝试以层次结构格式打印以下字典

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

如下所示:

60817401030000
    60817401030100
        60817401030200
            60817401030400
                60817401030500
                    60817401030600

到目前为止,我有以下代码可以使用但我必须在每一行中手动输入第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)

5 个答案:

答案 0 :(得分:5)

你可以试试这个:

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

for i, val in enumerate(fam_dict['6081740103']):
    print(' ' * i * 4 + val)

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

60817401030000
    60817401030100
        60817401030200
            60817401030300
                60817401030400
                    60817401030500
                        60817401030600

答案 1 :(得分:2)

您可以创建一个存储您正在迭代的行的变量,然后每次循环时递增该变量。您可以将该变量乘以\ t哪个是制表符操作符,以便控制所需的选项卡数。这是一个例子:

lines = 0

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

for k, val in fam_dict.items():
   for v in val:
       lines += 1
       t = '\t'
       t = t * lines
       print(t + str(v))

这是你的输出:

60817401030000
    60817401030100
        60817401030200
            60817401030300
                60817401030400
                    60817401030500
                        60817401030600

答案 2 :(得分:1)

以下是一个例子:

fam_dict = {'6081740103':['60817401030000','60817401030100','60817401030200','60817401030300','60817401030400','60817401030500','60817401030600']}
for k, v in fam_dict.items():
    for i, s in enumerate(v):
        print("%s%s"% ("\t"*i, s))

如果您想为其创建节点:

fam_dict = {'6081740103':['60817401030000','60817401030100','60817401030200','60817401030300','60817401030400','60817401030500','60817401030600']}
node_list = []
for k, v in fam_dict.items():
    last_parent = none
    for i, s in enumerate(v):
        print("%s%s"% ("\t"*i, s))
        node_list.append(node(v, last_parent))
        last_parent=node_list[-1]

父节点将为node_list[0]

答案 3 :(得分:1)

你也可以这样做。

for key in fam_dict.keys():
    for i in range(len(fam_dict[key])):
        print(i*"\t"+ fam_dict[key][i])

答案 4 :(得分:1)

试试这个:

fam_dict = {'6081740103':['60817401030000','60817401030100','60817401030200',
'60817401030300','60817401030400','60817401030500','60817401030600']}
l = fam_dict['6081740103']
for i in l:
   print(' '*l.index(i)*4+i)

输出:

60817401030000
    60817401030100
        60817401030200
            60817401030300
                60817401030400
                    60817401030500
                        60817401030600