def values(d): for v in d.values(): if isinstance(v, dict): for _ in values(v): yield _ else: yield v data = values({1:1,2:{2:2,3:{3:3,4:4}}}) for v in data: print(v) # 1 2 3 4
If using python 3.3+, replacefor _ in values(v): yield _可用yield from values(v)with
If using python 3.3+, replace
for _ in values(v): yield _
可用yield from values(v)
withIt seems that it can only be traversed