【发布时间】:2014-07-30 08:14:24
【问题描述】:
我为 JSON 创建了一个简单的深度树扫描(只有列表和值,没有对象):
def depth(x):
for e in x:
if type(e) == list:
for s in depth(e):
yield s
else:
yield(e)
建筑
for s in depth(e):
yield s
工作正常,但我不喜欢它。
是否有任何不错的方法来生成调用函数所生成的所有内容,而无需循环?
【问题讨论】:
-
我会避免特殊情况 - 总是处理一个序列或从不这样做 - 如果可能的话。
标签: python recursion tree yield