Created
June 20, 2018 13:00
-
-
Save cypreess/fc9ab6f991ab719bee0441ed23345f1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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