每日一练

44 阅读1分钟

每日一练20231228

按字典key的层深重新组合数据

def items(li, res=[]):
    tup = list()
    dic = {}
    for k, v in li.items():
        tup.append(k)
        if type(v) == dict:
            dic.update(v)

    res.append(tuple(tup))
    if len(dic) > 0:
        items(dic, res)

    return res


if __name__ == '__main__':
    d = {'a':
             {'b': {'c': {'d': 'h', 'j': 'i', 'o': {'p': 'q', 'r': 's'}, 't': 'u'}}, 'v': {'w': {'x': {'y': 'z'}}}},
         'e': {'f': {'i': 'k'}, 'm': 'n'}}

    print(items(d))