[英]'NoneType' is not iterable while recursively parsing a dict?
有了這個功能:
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
基於這篇博客 ,我認為問題是我正在使用的.get(j)
。 但是,如果我nested_dict.get(j) or {} for j in ['n', 'id']])
執行nested_dict.get(j) or {} 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']]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.