简体   繁体   English

从嵌套字典的子集字典创建字典

[英]creating a dictionary from a subset dicts of a nested dict

have to frame a new dict from a subset dicts of a dictionary with some dynamic control.必须从具有一些动态控制的字典的子集字典中构建一个新的字典。 input data:输入数据:

default_data = {
   'Common':{'name': "Trey", 'website': "http://treyhunner.com"},
   'api1_data':{
       'v1':{'name': "Anonymous User_v1", 'page_name': "Profile Page"},
       'v2': {'name': "Anonymous User_v2", 'page_name': "Profile Page"}},

   'api1_op': [],

   'api2_data':{
       'v1':{'name': "Anonymous User_v1", 'page_name': "Profile Page"},
       'v2': {'name': "Anonymous User_v2", 'page_name': "Profile Page"}},

   'api2_op': [],

   'api3_data':{'name1': "dmm", 'website1': "http://"}
}

User controls: versions of each api: based on the api version(v1/v2) it should form a dictionary with key as a parent key.用户控件:每个api的版本:基于api版本(v1/v2)应该形成一个以key为父key的字典。

Expected output:预期输出:

op= {'Common':{'name': "Trey", 'website': "http://treyhunner.com"},
     'api1_data': {'name': "Anonymous User_v1", 'page_name': "Profile Page"},
     'api1_op': [],
     'api2_data': {'name': "Anonymous User_v2", 'page_name': "Profile Page"},
     'api2_op': [],
     'api3_data':{'name1': "dmm", 'website1': "http://"}
}

The following code will take the user selected version and create a new dictionary according to his selection.以下代码将采用用户选择的版本并根据他的选择创建一个字典。

Please note, if the user entered not allowed version (not 'v1' or 'v2') it will raise RuntimeError .请注意,如果用户输入不允许的版本(不是“v1”或“v2”),它将引发 RuntimeError

api_allowed_version = {'v1', 'v2'}
default_data = {
   'Common':{'name': "Trey", 'website': "http://treyhunner.com"},
   'api1_data':{
       'v1':{'name': "Anonymous User_v1", 'page_name': "Profile Page"},
       'v2': {'name': "Anonymous User_v2", 'page_name': "Profile Page"}},

   'api1_op': [],

   'api2_data':{
       'v1':{'name': "Anonymous User_v1", 'page_name': "Profile Page"},
       'v2': {'name': "Anonymous User_v2", 'page_name': "Profile Page"}},

   'api2_op': [],

   'api3_data':{'name1': "dmm", 'website1': "http://"}
}

user_api_version = input('Please choose version (v1/v2): ')
if user_api_version not in api_allowed_version:
    raise RuntimeError('Not allowed api version ({0})'.format(user_api_version))

op = dict(default_data)
for key in op.keys():
   if key.endswith('_data') and user_api_version in default_data[key].keys():
        op[key] = default_data[key][user_api_version]

This will result with:这将导致:

{'Common': {'name': 'Trey', 'website': 'http://treyhunner.com'}, 
'api1_data': {'name': 'Anonymous User_v1', 'page_name': 'Profile Page'}, 
'api1_op': [], 
'api2_data': {'name': 'Anonymous User_v1', 'page_name': 'Profile Page'}, 
'api2_op': [], 
'api3_data': {'name1': 'dmm', 'website1': 'http://'}}

Due to the fact that this is python there is a need to create a deepcopy of the original dictionary (in this case the default_data ).由于这是python,因此需要创建原始字典的深层副本(在本例中为default_data )。 This can be done as in my example ( op = dict(default_data) ) or by这可以在我的示例中完成( op = dict(default_data) )或通过

import copy
op = copy.deepcopy(default_data)

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

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