简体   繁体   English

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

[英]How to add multiple values to a multilevel dictionary in python

I tried rubberduckying this, but I give up. 我试过这个,但我放弃了。 I don't understand why this keeps failing me. 我不明白为什么这总是让我失望。 I have the following dictionary template I want to fill 我要填写以下字典模板

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

I have the following code that triest to insert multiple values into it. 我有以下代码尝试向其中插入多个值。 This represents the code that I'm currently working with. 这代表了我目前正在使用的代码。

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

This keeps resulting in the code overwriting testfile every loop. 这会导致代码在每个循环中覆盖测试文件。 But i want to add the values and basically getting 3 location_names with 5 document_titles/page_nrs per location_name. 但是我想添加值并基本上得到3个location_name,每个location_name具有5个document_titles / page_nrs。

How do i achieve this? 我该如何实现?

EDIT 编辑

The desired output would be 所需的输出将是

{'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'}}
  }

Here I've solved like making a list of all dicts : 在这里,我像列出所有字典一样解决了问题:

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)

It O/Ps like : O / P像:

[{'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'}}}]

Is this what you want? 这是你想要的吗?

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"
        }
    }
}

I fixed the issue using this piece of code. 我使用这段代码解决了这个问题。 I basically added lists in the dictionary to make everything work. 我基本上在字典中添加了列表,以使所有内容正常工作。

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

output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM