Python 如何将嵌套的dict键和值平铺到其类型和变量的平铺列表中?

Python 如何将嵌套的dict键和值平铺到其类型和变量的平铺列表中?,python,python-3.x,dictionary,recursive-datastructures,Python,Python 3.x,Dictionary,Recursive Datastructures,给定以下dict类型(表示树): 如何将其转换为树路径的平面列表 ['Expr', 'Call', 'Name', "print"] ['Expr', 'Call', 'Name', 'Load'] ['Expr', 'Call', 'BinOp', 'Num', 1] ['Expr', 'Call', 'BinOp', 'Num', 'Add'] ['Expr', 'Call', 'BinOp', 'Num', 2] 换句话说,我想得到的是所有可能的树木路径,比如: 您可以对生成器使用递归:

给定以下dict类型(表示树):

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

['Expr', 'Call', 'Name', "print"]
['Expr', 'Call', 'Name', 'Load']
['Expr', 'Call', 'BinOp', 'Num', 1]
['Expr', 'Call', 'BinOp', 'Num', 'Add']
['Expr', 'Call', 'BinOp', 'Num', 2]
换句话说,我想得到的是所有可能的树木路径,比如:


您可以对生成器使用递归:

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']]

@user11628570是的,您需要做的就是更新该列表,使其包含子目录存储您的目标值的键。@user11628570您必须使用输入中可能出现的不同语法标记来更新“展平”第1行的列表。请看我最近的编辑,因为我已经为你的新输入做了这项工作,并且已经发布了结果。
[['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']]