我正在尝试以层次结构格式打印以下字典
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)
最佳答案
你可以试试这个:
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
关于python - 遍历字典值并随后打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50809220/