递归子生成器调用不执行
Recursive sub-generator call does not execute
Python 3.6+ 顺便说一句
如果子值与字符串匹配,我想从嵌套字典打印父值。为此,我尝试了这个递归调用
for child in traverse_dict(value, 'ready', 'y'):
print(child)
也就是说,当找到父键时,我想(递归地)检查它的任何子值(和子键)是否与相应的字符串模式匹配,如果是,则打印。 (我最终会收集这些值)
使用相同的调用但在函数之外按预期工作(尝试示例代码的最后两行或见下文)。
for i in traverse_dict(ex, 'ready', 'y'):
print(i)
# Output is
# Y
但是当我尝试
for i in traverse_dict(ex, 'id'):
print(i)
# Output is
# checkpoint
# A
# checkpoint
# B
但我希望
# Output is
# checkpoint
# Y <= (missing this, output of traverse_dict(ex, 'ready', 'y'))
# A
# checkpoint
# B
知道为什么在函数内部调用时 失败 吗?
看这个例子
ex = [
{
'Id': 'A',
'Status': { 'Ready': 'Y' }
},
{
'Id': 'B',
'Status': { 'Ready': 'N' }
}
]
def traverse_dict(
data,
*args
):
if isinstance(data, dict):
for key, value in data.items():
# FOR SUB-GENERATOR WHERE *ARGS IS PASSED 2 ARGS
try:
if key.lower() == args[0] and value.lower() == args[1]:
yield value
else:
yield from traverse_dict(value, *args)
except:
if key.lower() == args[0]:
print('checkpoint')
for child in traverse_dict(value, 'ready', 'y'):
print(child)
yield value
else:
yield from traverse_dict(value, *args)
elif isinstance(data, list):
for item in data:
yield from traverse_dict(item, *args)
for i in traverse_dict(ex, 'id'):
print(i)
for i in traverse_dict(ex, 'ready', 'y'):
print(i)
提前致谢!
您在 for child in traverse_dict(value, 'ready', 'y'):...
行传递了错误的数据。在您的情况下,value
包含“A”,因为当键为 Id
且值为 A
或 [=17= 时,key.lower() == args[0]
语句为 True
].您应该传递当前字典的 'Status'
成员。
表示正确行:for child in traverse_dict(data['Status'], 'ready', 'y'):
这一行的输出:
>>> python3 test.py
checkpoint
Y
A
checkpoint
B
Y
Python 3.6+ 顺便说一句
如果子值与字符串匹配,我想从嵌套字典打印父值。为此,我尝试了这个递归调用
for child in traverse_dict(value, 'ready', 'y'):
print(child)
也就是说,当找到父键时,我想(递归地)检查它的任何子值(和子键)是否与相应的字符串模式匹配,如果是,则打印。 (我最终会收集这些值)
使用相同的调用但在函数之外按预期工作(尝试示例代码的最后两行或见下文)。
for i in traverse_dict(ex, 'ready', 'y'):
print(i)
# Output is
# Y
但是当我尝试
for i in traverse_dict(ex, 'id'):
print(i)
# Output is
# checkpoint
# A
# checkpoint
# B
但我希望
# Output is
# checkpoint
# Y <= (missing this, output of traverse_dict(ex, 'ready', 'y'))
# A
# checkpoint
# B
知道为什么在函数内部调用时 失败 吗?
看这个例子
ex = [
{
'Id': 'A',
'Status': { 'Ready': 'Y' }
},
{
'Id': 'B',
'Status': { 'Ready': 'N' }
}
]
def traverse_dict(
data,
*args
):
if isinstance(data, dict):
for key, value in data.items():
# FOR SUB-GENERATOR WHERE *ARGS IS PASSED 2 ARGS
try:
if key.lower() == args[0] and value.lower() == args[1]:
yield value
else:
yield from traverse_dict(value, *args)
except:
if key.lower() == args[0]:
print('checkpoint')
for child in traverse_dict(value, 'ready', 'y'):
print(child)
yield value
else:
yield from traverse_dict(value, *args)
elif isinstance(data, list):
for item in data:
yield from traverse_dict(item, *args)
for i in traverse_dict(ex, 'id'):
print(i)
for i in traverse_dict(ex, 'ready', 'y'):
print(i)
提前致谢!
您在 for child in traverse_dict(value, 'ready', 'y'):...
行传递了错误的数据。在您的情况下,value
包含“A”,因为当键为 Id
且值为 A
或 [=17= 时,key.lower() == args[0]
语句为 True
].您应该传递当前字典的 'Status'
成员。
表示正确行:for child in traverse_dict(data['Status'], 'ready', 'y'):
这一行的输出:
>>> python3 test.py
checkpoint
Y
A
checkpoint
B
Y