您可以将递归与生成器一起使用:
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']]