开发手册 欢迎您!
软件开发者资料库

Python 通过拆分字典的key创建嵌套字典的方法及示例代码

本文主要介绍Python中,通过拆分字典中的key,创建一个多级嵌套字典的方法,以及相关的示例代码。

示例字典:

flat = {'X_a_one': 10,
'X_a_two': 20,
'X_b_one': 10,
'X_b_two': 20,
'Y_a_one': 10,
'Y_a_two': 20,
'Y_b_one': 10,
'Y_b_two': 20}

嵌套字典:

nested = {'X': {'a': {'one': 10,
'two': 20},
'b': {'one': 10,
'two': 20}},
'Y': {'a': {'one': 10,
'two': 20},
'b': {'one': 10,
'two': 20}}}

1、使用自定义函数实现

def nest_dict(flat):    result = {}    for k, v in flat.items():        _nest_dict_rec(k, v, result)    return resultdef _nest_dict_rec(k, v, out):    k, *rest = k.split('_', 1)    if rest:        _nest_dict_rec(rest[0], v, out.setdefault(k, {}))    else:        out[k] = vflat = {'X_a_one': 10,        'X_a_two': 20,         'X_b_one': 10,        'X_b_two': 20,         'Y_a_one': 10,        'Y_a_two': 20,        'Y_b_one': 10,        'Y_b_two': 20}print(nest_dict(flat))

2、使用for循环实现

output = {}source = {'X_a_one': 10,        'X_a_two': 20,         'X_b_one': 10,        'X_b_two': 20,         'Y_a_one': 10,        'Y_a_two': 20,        'Y_b_one': 10,        'Y_b_two': 20}for k, v in source.items():    current = output    pieces = k.split('_')    for piece in pieces[:-1]:       if not piece in current:          current[piece] = {}       current = current[piece]    current[pieces[-1]] = vprint(output)

3、使用collections.defaultdict实现

from collections import defaultdictfrom functools import reducefrom operator import getitemdef getFromDict(dataDict, mapList):    """Iterate nested dictionary"""    return reduce(getitem, mapList, dataDict)# instantiate nested defaultdict of defaultdictstree = lambda: defaultdict(tree)d = tree()flat = {'X_a_one': 10,        'X_a_two': 20,         'X_b_one': 10,        'X_b_two': 20,         'Y_a_one': 10,        'Y_a_two': 20,        'Y_b_one': 10,        'Y_b_two': 20}# iterate input dictionaryfor k, v in flat.items():    *keys, final_key = k.split('_')    getFromDict(d, keys)[final_key] = vprint(d)#最后一步,可以将defaultdict转换为常规dict,可能这一步有时没有必要。def default_to_regular_dict(d):    """Convert nested defaultdict to regular dict of dicts."""    if isinstance(d, defaultdict):        d = {k: default_to_regular_dict(v) for k, v in d.items()}    return d# 转换回常规字典res = default_to_regular_dict(d)print(res)