将词典列表转换为嵌套词典列表

时间:2020-06-04 19:20:09

标签: python python-2.7 dictionary data-structures

我尝试了上述问题的多个解决方案,但找不到适合我情况的解决方案。 我有以下词典列表。

readonly

并且我需要通过以下方式将此列表转换为嵌套词典列表

Future<String> generateImageHash(File file) async{
  Digest digest = await sha256.bind(file).openRead()).first;
  print("This is image Digest :  ${digest.toString()}");
  return  digest.toString();
}

我尝试过的解决方案如下

input_list = [
                {'k0':'v0','level':0,'row':0},
                {'k1':'v1','level':1,'row':1},
                {'k2':'v2','level':2,'row':2},
                {'k3':'v3','level':2,'row':3},
                {'k4':'v4','level':1,'row':4},
                {'k5':'v5','level':2,'row':5},
                {'k6':'v6','level':1,'row':6},
                {'k7':'v7','level':0,'row':7},
                {'k8':'v8','level':1,'row':8},
                {'k9':'v9','level':2,'row':9},
            ]

这使我的词典具有不同的级别,但是我无法通过此代码找到所需的解决方案

任何帮助将不胜感激。谢谢!

2 个答案:

答案 0 :(得分:3)

您可以对itertools.groupby使用递归:

from itertools import groupby as gb
data = [{'k0': 'v0', 'level': 0, 'row': 0}, {'k1': 'v1', 'level': 1, 'row': 1}, {'k2': 'v2', 'level': 2, 'row': 2}, {'k3': 'v3', 'level': 2, 'row': 3}, {'k4': 'v4', 'level': 1, 'row': 4}, {'k5': 'v5', 'level': 2, 'row': 5}, {'k6': 'v6', 'level': 1, 'row': 6}, {'k7': 'v7', 'level': 0, 'row': 7}, {'k8': 'v8', 'level': 1, 'row': 8}, {'k9': 'v9', 'level': 2, 'row': 9}]
_d = [{a:b for a, b in i.items() if a not in {'row'}} for i in data]

def get_results(d, l = 0):
  r, p = [(a, list(b)) for a, b in gb(d, key=lambda x:x['level'] == l)], []
  for a, b in r:
     if a:
        p.extend(b)
     else:      
        p[-1]['child'] = get_results(b, l+1)
  return p

import json
print(json.dumps(get_results(_d), indent=4))

输出:

[
  {
    "k0": "v0",
    "level": 0,
    "child": [
        {
            "k1": "v1",
            "level": 1,
            "child": [
                {
                    "k2": "v2",
                    "level": 2
                },
                {
                    "k3": "v3",
                    "level": 2
                }
            ]
        },
        {
            "k4": "v4",
            "level": 1,
            "child": [
                {
                    "k5": "v5",
                    "level": 2
                }
            ]
        },
        {
            "k6": "v6",
            "level": 1
        }
    ]
 },
 {
    "k7": "v7",
    "level": 0,
    "child": [
        {
            "k8": "v8",
            "level": 1,
            "child": [
                {
                    "k9": "v9",
                    "level": 2
                }
            ]
         }
      ]
    }
]

答案 1 :(得分:3)

# This stores last dicts seen by their levels
dict_by_levels = [None] * len(input_list)

# To be returned; begins with the first "root"
output_list = [input_list[0]]

for prev_dict, cur_dict in zip(input_list, input_list[1:]):
    # Get previous and current level, and store them in levels dict
    prev_level, cur_level = prev_dict["level"], cur_dict["level"]
    dict_by_levels[prev_level] = prev_dict
    dict_by_levels[cur_level] = cur_dict

    # If we are currently in a deeper level, append to previous dict's child
    if cur_level > prev_level:
        prev_dict.setdefault("child", []).append(cur_dict) 

    # If we are currently in a shallower or equal level *and* the current level is not 0,
    # then get the last one-level-before dict seen so far, and append to it
    elif cur_level <= prev_level and cur_level != 0:
        dict_by_levels[cur_level-1].setdefault("child", []).append(cur_dict)

    # This else is equivalent to "if cur_level == 0" i.e. a "root" dict
    else:
        output_list.append(cur_dict)

我在循环的每一圈都跟踪两个级别,即先前和当前的命令。我还制作了dict_by_levels,它是按级别列出的词典列表;索引0、1、2 ..将具有该级别看到的最后一个字典(如果有)。如果我们的水平变浅或变小(例如从2变到2),这会很有帮助。

如果当前级别高于​​先前级别(例如1> 0),那么我们只需追加到当前字典的子级。

如果当前级别小于以前的级别,则意味着我们应该将级别为current level - 1的最后一次接受字典的子级追加(dict_by_levels在此起作用)。当然,如果当前级别为0,我们就不这样做-我们有一个新的根。

然后通过附加到要返回的输出列表,在else子句中处理该根大小写。