【问题标题】:Loop through dictionary values and print subsequently遍历字典值并随后打印
【发布时间】:2018-11-21 09:10:30
【问题描述】:

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

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)

【问题讨论】:

    标签: python dictionary for-loop classification hierarchy


    【解决方案1】:

    你可以试试这个:

    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
    

    【讨论】:

      【解决方案2】:

      您可以创建一个变量来存储您正在迭代的行,然后在每次循环中递增该变量。您可以将该变量乘以 \t 这是制表符运算符,以控制您想要多少个制表符。这是一个例子:

      行 = 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
      

      【讨论】:

        【解决方案3】:

        你也可以这样做。

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

        【讨论】:

          【解决方案4】:

          这是一个例子:

          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]

          【讨论】:

            【解决方案5】:

            试试这个:

            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
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-10-05
              • 2016-10-10
              • 1970-01-01
              • 2015-08-07
              • 1970-01-01
              • 1970-01-01
              • 2014-01-31
              相关资源
              最近更新 更多