Loop through all nested dictionary values?

前端 未结 12 1433
温柔的废话
温柔的废话 2020-11-22 09:16
for k, v in d.iteritems():
    if type(v) is dict:
        for t, c in v.iteritems():
            print \"{0} : {1}\".format(t, c)

I\'m trying to l

12条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:00

    Alternative iterative solution:

    def myprint(d):
        stack = d.items()
        while stack:
            k, v = stack.pop()
            if isinstance(v, dict):
                stack.extend(v.iteritems())
            else:
                print("%s: %s" % (k, v))
    

提交回复
热议问题