python - “NoneType”在递归解析字典时不可迭代?

标签 python dictionary recursion data-structures

有了这个功能:

def extract_flat_branch(nested_dict, c = []):
    for i in ['left', 'op', 'right', 'func', 'value', 'args', 
              'ctx', 'body', 'comparators', 'ops', 'test', 'orelse', 'targets', 'slice']:
        if i in nested_dict:
            if isinstance(nested_dict[i], list):
                for b in nested_dict[i]:
                    yield from extract_flat_branch(b, c+[nested_dict['_type']]) 
            else:
                yield from extract_flat_branch(nested_dict[i], c+[nested_dict['_type']]) 
    lis = [c+[nested_dict['_type'], i] for i in filter(None, [nested_dict.get(j) for j in ['n', 'id']])]
    yield from lis if lis else [c+[nested_dict['_type']]] if len(nested_dict) == 1 else []

我正在遍历并提取表示为这本字典 ( my_dict ) 的树的分支,作为字符串列表,其中树的所有分支都是平面列表的元素。但是,出于某种原因,当我这样做时:

在:

print(list(extract_flat_branch(my_dict)))

而不是得到类似这样的输出:

[[node_1, node_2, .., node_n],[node_1, node_2, .., node_n],...,[node_1, node_2, .., node_n]]

我得到:

TypeError: argument of type 'NoneType' is not iterable

基于此blog ,我认为问题出在我正在使用的 .get(j) 上。但是,如果我执行 nested_dict.get(j) 或 {} for j in ['n', 'id']]),我仍然得到相同的 TypeError。知道如何解决这个问题并获得树分支的平面列表吗?

这是完整跟踪:

TypeError                                 Traceback (most recent call last)
<ipython-input-4-51bfd7bbf4e4> in <module>
      1 for i,j in enumerate(a_lis):
      2     print(i)
----> 3     print(list(extract_flat_branch(j)))

~/dir/util.py in extract_flat_branch(nested_dict, c)
     38             if isinstance(nested_dict[i], list):
     39                 for b in nested_dict[i]:
---> 40                     yield from extract_flat_branch(b, c+[nested_dict['_type']])
     41             else:
     42                 yield from extract_flat_branch(nested_dict[i], c+[nested_dict['_type']])

~/dir/util.py in extract_flat_branch(nested_dict, c)
     38             if isinstance(nested_dict[i], list):
     39                 for b in nested_dict[i]:
---> 40                     yield from extract_flat_branch(b, c+[nested_dict['_type']])
     41             else:
     42                 yield from extract_flat_branch(nested_dict[i], c+[nested_dict['_type']])

~/dir/util.py in extract_flat_branch(nested_dict, c)
     40                     yield from extract_flat_branch(b, c+[nested_dict['_type']])
     41             else:
---> 42                 yield from extract_flat_branch(nested_dict[i], c+[nested_dict['_type']])
     43     lis = [c+[nested_dict['_type'], i] for i in filter(None, [nested_dict.get(j) for j in ['n', 'id']])]
     44     yield from lis if lis else [c+[nested_dict['_type']]] if len(nested_dict) == 1 else []

~/dir/util.py in extract_flat_branch(nested_dict, c)
     38             if isinstance(nested_dict[i], list):
     39                 for b in nested_dict[i]:
---> 40                     yield from extract_flat_branch(b, c+[nested_dict['_type']])
     41             else:
     42                 yield from extract_flat_branch(nested_dict[i], c+[nested_dict['_type']])

~/dir/util.py in extract_flat_branch(nested_dict, c)
     40                     yield from extract_flat_branch(b, c+[nested_dict['_type']])
     41             else:
---> 42                 yield from extract_flat_branch(nested_dict[i], c+[nested_dict['_type']])
     43     lis = [c+[nested_dict['_type'], i] for i in filter(None, [nested_dict.get(j) for j in ['n', 'id']])]
     44     yield from lis if lis else [c+[nested_dict['_type']]] if len(nested_dict) == 1 else []

~/dir/util.py in extract_flat_branch(nested_dict, c)
     35     for i in ['left', 'op', 'right', 'func', 'value', 'args', 
     36               'ctx', 'body', 'comparators', 'ops', 'test', 'orelse', 'targets', 'slice']:
---> 37         if i in nested_dict:
     38             if isinstance(nested_dict[i], list):
     39                 for b in nested_dict[i]:

TypeError: argument of type 'NoneType' is not iterable

最佳答案

出现 NoneType 错误是因为在您的数据中有一个特定的 "value" 键,该键被散列为相应的 None“value” 是函数签名下方键列表中的目标键,当前逻辑尝试检查函数输入 nested_dict 中是否存在任何此类键。对于具有 None 值的目标键,您想要的输出是什么是相当不清楚的,但一个简单的解决方法是检查并忽略这种情况:

def extract_flat_branch(nested_dict, c = []):
   for i in ['left', 'op', 'right', 'func', 'value', 'args', 'ctx', 'body', 'comparators', 'ops', 'test', 'orelse', 'targets', 'slice', 'n', 'id']:
      if i in nested_dict:
        if isinstance(nested_dict[i], list):
            for b in nested_dict[i]:
                yield from extract_flat_branch(b, c+[nested_dict['_type']]) 
        elif isinstance(nested_dict[i], dict): #simple check here
            yield from extract_flat_branch(nested_dict[i], c+[nested_dict['_type']]) 
        else:
            yield c+[nested_dict[i]]


print(list(extract_flat_branch(data)))

输出:

[['FunctionDef', 'Assign', 'Call', 'Name', 'Load'], ['FunctionDef', 'Assign', 'Call', 'Name', 'list'], ['FunctionDef', 'Assign', 'Call', 'Name', 'Load'], ['FunctionDef', 'Assign', 'Call', 'Name', 'items'], ['FunctionDef', 'Assign', 'Name', 'Store'], ['FunctionDef', 'Assign', 'Name', 'items'], ['FunctionDef', 'Assign', 'Call', 'Name', 'Load'], ['FunctionDef', 'Assign', 'Call', 'Name', 'next_power_of_two'], ['FunctionDef', 'Assign', 'Call', 'Call', 'Name', 'Load'], ['FunctionDef', 'Assign', 'Call', 'Call', 'Name', 'int'], ['FunctionDef', 'Assign', 'Call', 'Call', 'BinOp', 'Num', 1.2], ['FunctionDef', 'Assign', 'Call', 'Call', 'BinOp', 'Mult'], ['FunctionDef', 'Assign', 'Call', 'Call', 'BinOp', 'Call', 'Name', 'Load'], ['FunctionDef', 'Assign', 'Call', 'Call', 'BinOp', 'Call', 'Name', 'len'], ['FunctionDef', 'Assign', 'Call', 'Call', 'BinOp', 'Call', 'Name', 'Load'], ['FunctionDef', 'Assign', 'Call', 'Call', 'BinOp', 'Call', 'Name', 'items'], ['FunctionDef', 'Assign', 'Name', 'Store'], ['FunctionDef', 'Assign', 'Name', 'size'], ['FunctionDef', 'Assign', 'BinOp', 'List', 'Load'], ['FunctionDef', 'Assign', 'BinOp', 'Mult'], ['FunctionDef', 'Assign', 'BinOp', 'Name', 'Load'], ['FunctionDef', 'Assign', 'BinOp', 'Name', 'size'], ['FunctionDef', 'Assign', 'Name', 'Store'], ['FunctionDef', 'Assign', 'Name', 'table'], ['FunctionDef', 'For', 'Assign', 'BinOp', 'Call', 'Name', 'Load'], ['FunctionDef', 'For', 'Assign', 'BinOp', 'Call', 'Name', 'hash_function'], ['FunctionDef', 'For', 'Assign', 'BinOp', 'Call', 'Name', 'Load'], ['FunctionDef', 'For', 'Assign', 'BinOp', 'Call', 'Name', 'i'], ['FunctionDef', 'For', 'Assign', 'BinOp', 'Mod'], ['FunctionDef', 'For', 'Assign', 'BinOp', 'Name', 'Load'], ['FunctionDef', 'For', 'Assign', 'BinOp', 'Name', 'size'], ['FunctionDef', 'For', 'Assign', 'Name', 'Store'], ['FunctionDef', 'For', 'Assign', 'Name', 'h'], ['FunctionDef', 'For', 'Assign', 'Name', 'Store'], ['FunctionDef', 'For', 'Assign', 'Name', 's'], ['FunctionDef', 'For', 'While', 'AugAssign', 'Add'], ['FunctionDef', 'For', 'While', 'AugAssign', 'Num', 1], ['FunctionDef', 'For', 'While', 'Assign', 'BinOp', 'BinOp', 'Name', 'Load'], ['FunctionDef', 'For', 'While', 'Assign', 'BinOp', 'BinOp', 'Name', 'h'], ['FunctionDef', 'For', 'While', 'Assign', 'BinOp', 'BinOp', 'Add'], ['FunctionDef', 'For', 'While', 'Assign', 'BinOp', 'BinOp', 'Name', 'Load'], ['FunctionDef', 'For', 'While', 'Assign', 'BinOp', 'BinOp', 'Name', 's'], ['FunctionDef', 'For', 'While', 'Assign', 'BinOp', 'Mod'], ['FunctionDef', 'For', 'While', 'Assign', 'BinOp', 'Name', 'Load'], ['FunctionDef', 'For', 'While', 'Assign', 'BinOp', 'Name', 'size'], ['FunctionDef', 'For', 'While', 'Assign', 'Name', 'Store'], ['FunctionDef', 'For', 'While', 'Assign', 'Name', 'h'], ['FunctionDef', 'For', 'While', 'Compare', 'Subscript', 'Name', 'Load'], ['FunctionDef', 'For', 'While', 'Compare', 'Subscript', 'Name', 'table'], ['FunctionDef', 'For', 'While', 'Compare', 'Subscript', 'Load'], ['FunctionDef', 'For', 'While', 'Compare', 'Subscript', 'Index', 'Name', 'Load'], ['FunctionDef', 'For', 'While', 'Compare', 'Subscript', 'Index', 'Name', 'h'], ['FunctionDef', 'For', 'While', 'Compare', None], ['FunctionDef', 'For', 'While', 'Compare', 'IsNot'], ['FunctionDef', 'For', 'Assign', 'Name', 'Load'], ['FunctionDef', 'For', 'Assign', 'Name', 'i'], ['FunctionDef', 'For', 'Assign', 'Subscript', 'Name', 'Load'], ['FunctionDef', 'For', 'Assign', 'Subscript', 'Name', 'table'], ['FunctionDef', 'For', 'Assign', 'Subscript', 'Store'], ['FunctionDef', 'For', 'Assign', 'Subscript', 'Index', 'Name', 'Load'], ['FunctionDef', 'For', 'Assign', 'Subscript', 'Index', 'Name', 'h'], ['FunctionDef', 'Return', 'Name', 'Load'], ['FunctionDef', 'Return', 'Name', 'table']]

更新的解决方案:

def extract_flat_branch(nested_dict, c = []):
  targets = {'left', 'op', 'right', 'func', 'value', 'args', 'ctx', 'body', 'comparators', 'ops', 'test', 'orelse', 'targets', 'slice', 'n', 'id', 'slice', 'annotation', 'arg', 'elts', 's', '_type'}
  for a, b in nested_dict.items():
     if a in targets:
        if isinstance(b, dict):
           yield from extract_flat_branch(b, c+[a])
        elif isinstance(b, list):
           for i in b:
              yield from extract_flat_branch(i, c+[a])
        else:
            yield c+[b]

print(list(extract_flat_branch(data)))

输出:

[['FunctionDef'], ['args', 'arguments'], ['args', 'args', 'arg'], ['args', 'args', None], ['args', 'args', 'self'], ['body', 'Expr'], ['body', 'value', 'Str'], ['body', 'value', 'Like items(), but with all lowercase keys.'], ['body', 'Return'], ['body', 'value', 'GeneratorExp']]

关于python - “NoneType”在递归解析字典时不可迭代?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57275492/

相关文章:

python - 通过 if 语句进行条件字典查找的问题

recursion - 使用基于堆栈的深度优先搜索查找循环

python - 使用Python从Grakn获取数据

python - 使用字典中的键反转多个值

python - 将具有相同哈希值的两个键放入字典中

recursion - Lisp 递归不调用以前的函数

c++ - 通过 m x n 网格的游览次数?

python - 我如何安装初始模型?

python - Flask 子域与 Heroku 和 Godaddy(SERVER_NAME 问题)

python - 在 Python 中编辑列表内容