我知道这是一个老问题,但我正在寻找一种简单、干净的方法来遍历嵌套字典,这是我有限的搜索中最接近的东西。如果你想要更多的信息而不仅仅是文件名,oadams的答案就不够用了,而spicavigo的答案看起来很复杂。
我最终自己编写了一个类似于os.walk处理目录的函数,但它返回所有键/值信息。
它返回一个迭代器,对于嵌套字典"tree"中的每个目录,迭代器返回(path, sub-dicts, values),其中:
- path是字典的路径
- sub-dicts是该字典中每个子字典的(key, dict)对的元组
- values是该字典中每个(非字典)项的(key, value)对的元组
def walk(d):
'''
Walk a tree (nested dicts).
For each 'path', or dict, in the tree, returns a 3-tuple containing:
(path, sub-dicts, values)
where:
* path is the path to the dict
* sub-dicts is a tuple of (key,dict) pairs for each sub-dict in this dict
* values is a tuple of (key,value) pairs for each (non-dict) item in this dict
'''
nested_keys = tuple(k for k in d.keys() if isinstance(d[k],dict))
items = tuple((k,d[k]) for k in d.keys() if k not in nested_keys)
yield ('/', [(k,d[k]) for k in nested_keys], items)
for k in nested_keys:
for res in walk(d[k]):
res = ('/%s' % k + res[0], res[1], res[2])
yield res
以下是我用来测试的代码,虽然其中还有一些无关但很棒的东西:
import simplejson as json
from collections import defaultdict
tree = lambda: defaultdict(tree)
def walk(d):
'''
Walk a tree (nested dicts).
For each 'path', or dict, in the tree, returns a 3-tuple containing:
(path, sub-dicts, values)
where:
* path is the path to the dict
* sub-dicts is a tuple of (key,dict) pairs for each sub-dict in this dict
* values is a tuple of (key,value) pairs for each (non-dict) item in this dict
'''
nested_keys = tuple(k for k in d.keys() if isinstance(d[k],dict))
items = tuple((k,d[k]) for k in d.keys() if k not in nested_keys)
yield ('/', [(k,d[k]) for k in nested_keys], items)
for k in nested_keys:
for res in walk(d[k]):
res = ('/%s' % k + res[0], res[1], res[2])
yield res
mem = tree()
root = mem['SomeRootDirectory']
root['foo.txt'] = None
root['bar.txt'] = None
root['Stories']['Horror']['scary.txt'] = None
root['Stories']['Horror']['Trash']['notscary.txt'] = None
root['Stories']['Cyberpunk']
root['Poems']['doyoureadme.txt'] = None
s = json.dumps(mem, indent=2)
print s
print
for (path, dicts, items) in walk(json.loads(s)):
print '[%s]' % path
for key,val in items:
print '%s = %s' % (path+key,val)
print
输出的结果如下所示:
{
"SomeRootDirectory": {
"foo.txt": null,
"Stories": {
"Horror": {
"scary.txt": null,
"Trash": {
"notscary.txt": null
}
},
"Cyberpunk": {}
},
"Poems": {
"doyoureadme.txt": null
},
"bar.txt": null
}
}
[/]
[/SomeRootDirectory/]
/SomeRootDirectory/foo.txt = None
/SomeRootDirectory/bar.txt = None
[/SomeRootDirectory/Stories/]
[/SomeRootDirectory/Stories/Horror/]
/SomeRootDirectory/Stories/Horror/scary.txt = None
[/SomeRootDirectory/Stories/Horror/Trash/]
/SomeRootDirectory/Stories/Horror/Trash/notscary.txt = None
[/SomeRootDirectory/Stories/Cyberpunk/]
[/SomeRootDirectory/Poems/]
/SomeRootDirectory/Poems/doyoureadme.txt = None