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

用户名

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

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

换句话说,我想要得到的是树的所有可能路径,如下所示:

字典的树形表示

阿贾克斯1234

您可以将递归与生成器一起使用:

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

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

如何快速从键列表和列表值构建python字典?

比较Python中嵌套字典的键和值

如何将变量的值保持多次推入同一列表中?

如何在Google Chrome本地存储同步中设置变量键和值?

如何訪問列表和字典中的嵌套值以獲取數據?

Appsync和GraphQL:如何按嵌套值过滤列表

如何将大于和小于添加到批处理文件变量中

如何在python中以唯一键和多个值将输出作为字典返回?

如何将类的函数和变量传递给setInterval?

将列表值中的列表替换为dict值

使用serde从JSON将键和值列表反序列化为struct?

如何使用Python和Pytest比较和测试词典列表中的特定键

如何将JSON类型的字段嵌套到一堆新字段中?

如何将所有中间属性变量保留在深度嵌套的结构中

如何從嵌套列表中獲取列表並使用 zip 和 map 追加?

嵌套字典中的值的總和

如何获取类变量和类型提示?

如何将null键的null值添加到数组php中?

如何将地图列表转换为嵌套地图?

如何将整数和浮点值相乘并在Rust中将结果显示为浮点值?

如何将python的Decimal()类型转换为INT和指数

如何将列表/元组传递到Django的环境变量中

将多列收集到一个键和两个值中

使用 C# 和 Lambda,如何從嵌套 List<> 的屬性中獲取不同的列表?

当iOS中此变量包含Null时,如何将Any对象类型的变量转换为String?

如何将通用协议用作变量类型

如何将链接/网页返回的值存储在php变量中

在Python的列表中搜索包含“ <”和“>”的值

如何针对文件中的一个占位符编写字典键和值

TOP lista

quentelabel

Arquivo