Skip to content

Instantly share code, notes, and snippets.

@cypreess
Created June 20, 2018 13:00
Show Gist options
  • Save cypreess/fc9ab6f991ab719bee0441ed23345f1e to your computer and use it in GitHub Desktop.
Save cypreess/fc9ab6f991ab719bee0441ed23345f1e to your computer and use it in GitHub Desktop.
from collections import OrderedDict
def deep_search(needles, haystack):
found = {}
if type(needles) != type([]):
needles = [needles]
if type(haystack) == type(OrderedDict()) or type(haystack) == type(dict()):
for needle in needles:
if needle in haystack.keys():
found[needle] = haystack[needle]
elif len(haystack.keys()) > 0:
for key in haystack.keys():
result = deep_search(needle, haystack[key])
if result:
for k, v in result.items():
found[k] = v
elif type(haystack) == type([]):
for node in haystack:
result = deep_search(needles, node)
if result:
for k, v in result.items():
found[k] = v
return found
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment