BFS and DFS in Dict Traversal
I answered such a question on Stack Overflow: How do I extract all keys from this JSON file?
Retrieve all the dict keys from a dict whose values consist of multiple Iterable obj.
data = {
'k1': 'Lanbao',
'k2': {
'k3': 'Lanbao',
'k4': [
'Lanbao',
{
'k5': 'Lanbao',
'k6': ['Lanbao']
}
],
'k7': 'Lanbao',
},
'k8': 'Lanbao'
}
By constructing generators to implement BFS (Breadth-First Search) and DFS (Depth-First Search) respectively.
bfs.py
from collections import deque
from collections.abc import Iterable
def get_keys(data, ignore_type=(str, bytes)):
deque_ = deque([data])
while deque_:
node = deque_.popleft()
if isinstance(node, dict):
for k, v in node.items():
yield k
deque_.append(v)
elif isinstance(node, Iterable) and not isinstance(node, ignore_type):
for i in node:
deque_.append(i)