- def flatten(dictionary):
- stack = [((), dictionary)]
- result = {}
- while stack:
- path, current = stack.pop()
- if not current:
- result["/".join((path ))] = ""
- for k, v in current.items():
- if isinstance(v, dict):
- stack.append((path + (k,), v))
- else:
- result["/".join((path + (k,)))] = v
- return result
- #该片段来自于http://www.codesnippet.cn/detail/1501201511542.html
来源: http://www.codesnippet.cn/detail/1501201511542.html