簡體   English   中英

如何將嵌套的 dict 鍵和值平鋪到它們的類型和變量的平面列表中?

[英]How to flat nested dict keys and values into flat lists of their types and variables?

給定以下 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]

換句話說,我想要得到的是像這樣的樹的所有可能路徑:

字典的樹表示

您可以將遞歸與生成器一起使用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM