如何在python中向多级字典添加多个值

时间:2018-03-05 12:00:49

标签: python dictionary dictionary-comprehension

我试过橡皮包装,但我放弃了。我不明白为什么这会让我失望。我有以下要填写的词典模板

 testfile = {'locations':
        {'location_name1":
                {'document_title1': 'title_value1',
                'page_nr1': 'text_value1'}
        }}

我有以下代码,可以在其中插入多个值。这代表了我目前正在使用的代码。

testfile = {}


locatie = 0


while location != 5:
   page_nr = 0
   while page_nr != 3:
      testfile = testfile + {'location':{'location_naam' + str(location): 
      {'page_nr' + str(page_nr):'text_value'+ str(pagina_nr)}}}
      page_nr += 1
   location += 1

这导致每个循环都会导致代码覆盖测试文件。但我想添加值,基本上每个location_name有3个location_names和5个document_titles / page_nrs。

我如何实现这一目标?

修改

所需的输出是

{'location': 
   {'location_naam0': {
      'page_nr0': 'text_value0',
      'page_nr1': 'text_value1',
      'page_nr1': 'text_value1'}}, 
   {'location_naam1': {
      'page_nr0': 'text_value0',
      'page_nr1': 'text_value1',
      'page_nr1': 'text_value1'}},
   {'location_naam2': {
      'page_nr0': 'text_value0',
      'page_nr1': 'text_value1',
      'page_nr1': 'text_value1'}},
   {'location_naam3': {
      'page_nr0': 'text_value0',
      'page_nr1': 'text_value1',
      'page_nr1': 'text_value1'}},
   {'location_naam4': {
      'page_nr0': 'text_value0',
      'page_nr1': 'text_value1',
      'page_nr1': 'text_value1'}}
  }

3 个答案:

答案 0 :(得分:2)

我在这里解决了所有决定的列表:

testfile = []

location = 0
while location != 5:
    page_nr = 0
    while page_nr != 3:
         testfile.append( {'location':{'location_naam' +str(location):
             {'page_nr' +str(page_nr):'text_value'+str(pagina_nr)}}})
         page_nr += 1
    location += 1
 print(testfile)

O / Ps喜欢:

[{'location': {'location_naam0': {'page_nr0': 'text_value0'}}}, 
 {'location': {'location_naam0': {'page_nr1': 'text_value1'}}}, 
 {'location': {'location_naam0': {'page_nr2': 'text_value2'}}}, 
 {'location': {'location_naam1': {'page_nr0': 'text_value0'}}},                          
 {'location': {'location_naam1': {'page_nr1': 'text_value1'}}}, 
 {'location': {'location_naam1': {'page_nr2': 'text_value2'}}},      
 {'location': {'location_naam2': {'page_nr0': 'text_value0'}}}, 
 {'location': {'location_naam2': {'page_nr1': 'text_value1'}}}, 
 {'location': {'location_naam2': {'page_nr2': 'text_value2'}}}, 
 {'location': {'location_naam3': {'page_nr0': 'text_value0'}}}, 
 {'location': {'location_naam3': {'page_nr1': 'text_value1'}}}, 
 {'location': {'location_naam3': {'page_nr2': 'text_value2'}}},
 {'location': {'location_naam4': {'page_nr0': 'text_value0'}}}, 
 {'location': {'location_naam4': {'page_nr1': 'text_value1'}}}, 
 {'location': {'location_naam4': {'page_nr2': 'text_value2'}}}]

答案 1 :(得分:2)

这是你想要的吗?

print({
    'location': {
        "location_naam{}".format(i): {
            "page_nr{}".format(k): "text_value{}".format(k)
            for k in range(2)
        }
        for i in range(4) 
    }

})


out:
{
    "location": {
        "location_naam0": {
            "page_nr0": "text_value0",
            "page_nr1": "text_value1"
        },
        "location_naam1": {
            "page_nr0": "text_value0",
            "page_nr1": "text_value1"
        },
        "location_naam2": {
            "page_nr0": "text_value0",
            "page_nr1": "text_value1"
        },
        "location_naam3": {
            "page_nr0": "text_value0",
            "page_nr1": "text_value1"
        }
    }
}

答案 2 :(得分:0)

我使用这段代码解决了这个问题。我基本上在字典中添加了列表以使一切正常。

while x != 4: # documentniveau
    results[0]['locaties'].append({'locatie' + str(x): []})
    y = 0
    while y != 2: # paginaniveau
        results[0]['locaties'][x]['locatie'+str(x)].append({'pagenr': y})
        y += 1
    x += 1

输出:

[
  {
    "locaties": [
      {
        "locatie0": [
          {
            "pagenr": 0
          },
          {
            "pagenr": 1
          }
        ]
      },
      {
        "locatie1": [
          {
            "pagenr": 0
          },
          {
            "pagenr": 1
          }
        ]
      },
      {
        "locatie2": [
          {
            "pagenr": 0
          },
          {
            "pagenr": 1
          }
        ]
      },
      {
        "locatie3": [
          {
            "pagenr": 0
          },
          {
            "pagenr": 1
          }
        ]
      }
    ]
  }
]