以干净的编程方式创建大量嵌套的python字典
我正在使用一系列函数来填充嵌套的字典。我想知道是否有比下面示例中所示的长赋值字符串更干净的方法来实现这一点以干净的编程方式创建大量嵌套的python字典,python,dictionary,nested,Python,Dictionary,Nested,我正在使用一系列函数来填充嵌套的字典。我想知道是否有比下面示例中所示的长赋值字符串更干净的方法来实现这一点 outputdict = {} outputdict['x']={} outputdict['x']['y']={} outputdict['x']['y']['total_patients']=len(example_dict.keys()) outputdict['x']['y']['z']={} for variable1 in variable1s: outputdict[
outputdict = {}
outputdict['x']={}
outputdict['x']['y']={}
outputdict['x']['y']['total_patients']=len(example_dict.keys())
outputdict['x']['y']['z']={}
for variable1 in variable1s:
outputdict['x']['y']['z'][str(variable1)]={}
outputdict['x']['y']['z'][str(variable1)]['total_patients']=function_1(example_dict, variable1).count()
for popn in ['total','male','female']:
outputdict['x']['y']['z'][str(variable1)][popn]={}
for age_bucket in np.linspace(40,60,5):
age_str = str(age_bucket)+'_'+str(age_bucket+5)
outputdict['x']['y']['z'][str(variable1)][popn][age_str]={}
outputdict['x']['y']['z'][str(variable1)][popn]["total"]={}
for res in restypes:
if popn == 'total':
codelist, ncodes = function_2(function_1(example_dict, variable1), res, age_bucket)
else:
codelist, ncodes = function_2_gender(function_1(example_dict, variable1), res, age_bucket, popn)
outputdict['x']['y']['z'][str(variable1)][popn][age_str][res]={}
outputdict['x']['y']['z'][str(variable1)][popn][age_str][res]['total_codes']=ncodes
outputdict['x']['y']['z'][str(variable1)][popn][age_str][res]['top_codes']=[]
for item in codelist:
disp = {"code": item[0][:2], "value":item[0][2], "count":item[1]}
outputdict['x']['y']['z'][str(variable1)][popn][age_str][res]['top_codes'].append(disp)
codelist, ncodes = list_top_codes(function_1(example_dict, variable1), res)
outputdict['x']['y']['z'][str(variable1)][popn]["total"][res]={}
outputdict['x']['y']['z'][str(variable1)][popn]["total"][res]['top_codes']=[]
for item in codelist:
disp = {"code": item[0][:2], "value":item[0][2], "count":item[1]}
outputdict['x']['y']['z'][str(variable1)][popn]["total"][res]['top_codes'].append(disp)
outputdict
你可以用它。这将允许您跳过空DICT的创建,因为在取消引用未定义的键时会自动创建空DICT:
from collections import defaultdict
dd = lambda: defaultdict(dd)
d = dd()
d['foo']['bar']['foobar'] = 1
因此,您的代码如下所示:
outputdict = dd()
outputdict['x']['y']['total_patients']=len(example_dict.keys())
for variable1 in variable1s:
outputdict['x']['y']['z'][str(variable1)]['total_patients']=function_1(example_dict, variable1).count()
另一个可能的改进是将嵌套字典存储到变量中,这样就不必到处键入完整路径:
for variable1 in variable1s:
nested = dd()
outputdict['x']['y']['z'][str(variable1)]=nested
nested['total_patients']=function_1(example_dict, variable1).count()
你可以用它。这将允许您跳过空DICT的创建,因为在取消引用未定义的键时会自动创建空DICT:
from collections import defaultdict
dd = lambda: defaultdict(dd)
d = dd()
d['foo']['bar']['foobar'] = 1
因此,您的代码如下所示:
outputdict = dd()
outputdict['x']['y']['total_patients']=len(example_dict.keys())
for variable1 in variable1s:
outputdict['x']['y']['z'][str(variable1)]['total_patients']=function_1(example_dict, variable1).count()
另一个可能的改进是将嵌套字典存储到变量中,这样就不必到处键入完整路径:
for variable1 in variable1s:
nested = dd()
outputdict['x']['y']['z'][str(variable1)]=nested
nested['total_patients']=function_1(example_dict, variable1).count()
您可以使用dict的defaultdict来避免字典初始化。 您可以将它们链接到任意远的位置,以形成嵌套字典层次结构。例如,这里有一个2级和3级层次字典
two_level = defaultdict(lambda: defaultdict(dict))
three_level = defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
您的字典现在如下:2级[1][2]
和3级[1][2][3]
,并且将是空的dicts{}
在您的例子中,似乎有一个4级嵌套,所以我可能会将outputdict初始化为:
output_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(dict))))
想不出在这里还可以做什么-如果可能的话,我建议您简化这个嵌套结构。您可以使用dict的defaultdict来避免字典初始化。 您可以将它们链接到任意远的位置,以形成嵌套字典层次结构。例如,这里有一个2级和3级层次字典
two_level = defaultdict(lambda: defaultdict(dict))
three_level = defaultdict(lambda: defaultdict(lambda: defaultdict(dict)))
您的字典现在如下:2级[1][2]
和3级[1][2][3]
,并且将是空的dicts{}
在您的例子中,似乎有一个4级嵌套,所以我可能会将outputdict初始化为:
output_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: defaultdict(dict))))
想不出在这里还可以做什么-如果可能的话,我建议您简化这个嵌套结构。为什么您的数据结构嵌套得如此荒谬?可能不需要。为什么数据结构嵌套得如此荒谬?也许不需要。