循环通过文本文件并尝试创建嵌套的字典,但该字典仅打印最后一个循环迭代,而不打印嵌套在一起的完整循环

时间:2018-09-20 08:24:35

标签: python dictionary for-loop nested

我正在尝试从一些for循环中创建一个嵌套的字典。如果我的代码是这样定位的:

    for figure in problem.figures:

        for object in problem.figures[figure].objects:
            attribute_dict_A = {}
            nested_dict = {}

            for k, v in problem.figures[figure].objects[object].attributes.items():
                attribute_dict_A[k] = v
                attribute_dict_A.update({'object': object})

                nested_dict[figure] = attribute_dict_A

        print(nested_dict)

然后输出显示整个循环,如下所示:

  

{'A':{'shape':'square','object':'a','fill':'yes','size':'非常   大'}}

     

{'B':{'shape':'square','object':'b','fill':'yes','size':'非常   大'}}

     

{'C':{'shape':'square','object':'c','fill':'yes','size':'非常   大'}}

     

{'1':{'shape':'pentagon','object':'d','fill':'yes','size':   '非常大'}}

     

{'2':{'shape':'正方形','object':'e','fill':'是','size':'非常   大'}}

     

{'3':{'shape':'triangle','object':'f','fill':'yes','size':   '非常大'}}

     

{'4':{'shape':'pac-man','object':'g','fill':'yes','size':'非常   大'}}

     

{'5':{'shape':'star','object':'h','fill':'yes','size':'非常   大'}}

     

{'6':{'shape':'heart','object':'i','fill':'是','size':'非常   大'}}

但是,如果我的代码正确缩进(请参见'print(nested_dict)'),那么它只会打印循环的最后一次迭代。

如何循环循环并存储所需的一切?

for figure in problem.figures:

        for object in problem.figures[figure].objects:
            attribute_dict_A = {}
            nested_dict = {}

            for k, v in problem.figures[figure].objects[object].attributes.items():
                attribute_dict_A[k] = v
                attribute_dict_A.update({'object': object})

                nested_dict[figure] = attribute_dict_A

print(nested_dict)

我的最终输出是这样的:

  

{'6':{'shape':'heart','object':'i','fill':'是','size':'非常   大'}}

编辑----

我将代码更新为此,但是仍然无法获得所需的结果。每当它遍历循环时,它似乎都覆盖了我的字典。

nested_dict = {}
attribute_dict = {}

    for figure in problem.figures:

        for object in problem.figures[figure].objects:



            for k, v in problem.figures[figure].objects[object].attributes.items():
                attribute_dict[k] = v
                attribute_dict.update({'object': object})

        nested_dict[figure] = attribute_dict

    pprint(nested_dict)

以下是我正在循环浏览的文本文件的示例:

大写字母A是数字,小写字母a是对象,k,v是属性对

A
    a
        shape:circle
        size:very large
        fill:no
    b
        shape:plus
        size:small
        fill:yes
        angle:0
       inside:a
B
    c
        shape:circle
        size:very large
        fill:no
    d
        shape:plus
        size:small
        fill:yes
        angle:0
        inside:c
C
    e
        shape:circle
        size:very large
        fill:no
    f
        shape:plus
        size:small
        fill:yes
        angle:0
        inside:e

1 个答案:

答案 0 :(得分:1)

您进行迭代,然后创建一个字典,因此每次迭代时,都会创建一个新的空字典,因此将nested_dict移出循环:

nested_dict = {}
for figure in problem.figures:

        for object in problem.figures[figure].objects:
            attribute_dict_A = {}

            for k, v in problem.figures[figure].objects[object].attributes.items():
                attribute_dict_A[k] = v
                attribute_dict_A.update({'object': object})

                nested_dict[figure] = attribute_dict_A

print(nested_dict)

顺便说一句,也许也应该重新定位attribute_dict_A

attribute_dict_A = {}
nested_dict = {}
for figure in problem.figures:

        for object in problem.figures[figure].objects:

            for k, v in problem.figures[figure].objects[object].attributes.items():
                attribute_dict_A[k] = v
                attribute_dict_A.update({'object': object})

                nested_dict[figure] = attribute_dict_A

print(nested_dict)