python - 如何将嵌套的字典键和值平坦化为其类型和变量的平坦列表?

标签 python python-3.x dictionary recursive-datastructures

给定以下字典类型(代表一棵树):

{'_type': 'Expr',
 'col_offset': 0,
 'lineno': 1,
 'value': {'_type': 'Call',
  'args': [{'_type': 'BinOp',
    'col_offset': 6,
    'left': {'_type': 'Num', 'col_offset': 6, 'lineno': 1, 'n': 1},
    'lineno': 1,
    'op': {'_type': 'Add'},
    'right': {'_type': 'Num', 'col_offset': 8, 'lineno': 1, 'n': 2}}],
  'col_offset': 0,
  'func': {'_type': 'Name',
   'col_offset': 0,
   'ctx': {'_type': 'Load'},
   'id': 'print',
   'lineno': 1},
  'keywords': [],
  'lineno': 1}}

如何将其转换为树路径的平面列表?:

['Expr', 'Call', 'Name', "print"]
['Expr', 'Call', 'Name', 'Load']
['Expr', 'Call', 'BinOp', 'Num', 1]
['Expr', 'Call', 'BinOp', 'Num', 'Add']
['Expr', 'Call', 'BinOp', 'Num', 2]

换句话说,我想要得到的是像这样的树的所有可能路径:

Tree representation of the dict

最佳答案

您可以使用带有生成器的递归:

d = {'_type': 'Expr', 'col_offset': 0, 'lineno': 1, 'value': {'_type': 'Call', 'args': [{'_type': 'BinOp', 'col_offset': 6, 'left': {'_type': 'Num', 'col_offset': 6, 'lineno': 1, 'n': 1}, 'lineno': 1, 'op': {'_type': 'Add'}, 'right': {'_type': 'Num', 'col_offset': 8, 'lineno': 1, 'n': 2}}], 'col_offset': 0, 'func': {'_type': 'Name', 'col_offset': 0, 'ctx': {'_type': 'Load'}, 'id': 'print', 'lineno': 1}, 'keywords': [], 'lineno': 1}}
def flatten(_d, c = []):
   for i in ['left', 'op', 'right', 'func', 'value', 'args', 'ctx', 'body', 'comparators', 'ops', 'test', 'orelse']:
       if i in _d:
           if isinstance(_d[i], list):
              for b in _d[i]:
                 yield from flatten(b, c+[_d['_type']]) 
           else:
              yield from flatten(_d[i], c+[_d['_type']]) 
   _j = [c+[_d['_type'], i] for i in filter(None, [_d.get(j) for j in ['n', 'id']])]
   yield from _j if _j else [c+[_d['_type']]] if len(_d) == 1 else []


print(list(flatten(d)))

输出:

[['Expr', 'Call', 'Name', 'Load'], 
 ['Expr', 'Call', 'Name', 'print'], 
 ['Expr', 'Call', 'BinOp', 'Num', 1], 
  ['Expr', 'Call', 'BinOp', 'Add'], 
  ['Expr', 'Call', 'BinOp', 'Num', 2]]

编辑:新输出:

[['FunctionDef', 'If', 'Expr', 'Call', 'Name', 'Load'], ['FunctionDef', 'If', 'Expr', 'Call', 'Name', 'print'], ['FunctionDef', 'If', 'Compare', 'Name', 'Load'], ['FunctionDef', 'If', 'Compare', 'Name', 'n'], ['FunctionDef', 'If', 'Compare', 'Lt'], ['FunctionDef', 'If', 'If', 'Return', 'Subscript', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Return', 'Subscript', 'Name', 'FibArray'], ['FunctionDef', 'If', 'If', 'Return', 'Subscript', 'Load'], ['FunctionDef', 'If', 'If', 'Compare', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Compare', 'Name', 'n'], ['FunctionDef', 'If', 'If', 'Compare', 'Call', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Compare', 'Call', 'Name', 'len'], ['FunctionDef', 'If', 'If', 'Compare', 'Call', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Compare', 'Call', 'Name', 'FibArray'], ['FunctionDef', 'If', 'If', 'Compare', 'LtE'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'Name', 'fibonacci'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'BinOp', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'BinOp', 'Name', 'n'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'BinOp', 'Sub'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'BinOp', 'Num', 1], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Add'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'Name', 'fibonacci'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'BinOp', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'BinOp', 'Name', 'n'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'BinOp', 'Sub'], ['FunctionDef', 'If', 'If', 'Assign', 'BinOp', 'Call', 'BinOp', 'Num', 2], ['FunctionDef', 'If', 'If', 'Expr', 'Call', 'Attribute', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Expr', 'Call', 'Attribute', 'Name', 'FibArray'], ['FunctionDef', 'If', 'If', 'Expr', 'Call', 'Attribute', 'Load'], ['FunctionDef', 'If', 'If', 'Expr', 'Call', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Expr', 'Call', 'Name', 'temp_fib'], ['FunctionDef', 'If', 'If', 'Return', 'Name', 'Load'], ['FunctionDef', 'If', 'If', 'Return', 'Name', 'temp_fib']]

关于python - 如何将嵌套的字典键和值平坦化为其类型和变量的平坦列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57208101/

相关文章:

Python:工厂属性

c# - 如何将字典转换为 ConcurrentDictionary?

c++ - 如何创建迭代器来解决这个问题?

python - Pandas 数据框问题。不知道如何清理和管理一种 block 状矩阵

python - 元素更改后列表不会更改

python - 使用 PyObject_CallMethod 调用带有可选参数的函数

python - 用换行符格式化 python 字典

python - 不使用 gtk 显示图像

python - 将字典添加到csv文件

python - pandas 添加sql表,检查表是否存在