Skip to content

Instantly share code, notes, and snippets.

@emmanuellyautomated
Last active September 3, 2016 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emmanuellyautomated/7405c0f186fe973ad9426b8b967519ba to your computer and use it in GitHub Desktop.
Save emmanuellyautomated/7405c0f186fe973ad9426b8b967519ba to your computer and use it in GitHub Desktop.
can determine if a search term is a value in a nested dictionary
def search_dict(d, term, nests=[]):
def last(array):
return array[-1]
'''
breadth-first search a dictionary
'''
presence = None
nests_at_this_level = [d.get(key) for key in d.keys() if type(d.get(key)).__name__ == 'dict']
lists_with_nests = [d.get(key) for key in d.keys()
if type(d.get(key)).__name__ == 'list'
and any(filter(lambda x: type(x).__name__ == 'dict', d.get(key)))
]
nests_from_lists = [item for list_of_items in lists_with_nests for item in list_of_items]
nests_at_this_level.extend(nests_from_lists)
if nests_at_this_level:
nests.append(nests_at_this_level)
else:
for val in d.values():
presence = True if term in val else None
if presence is not None:
return presence
if any(nests):
if any(last(nests)):
nest = last(nests).pop()
if last(nests)==[]:
nests.pop()
return search_dict(nest, term, nests)
else:
return False
@emmanuellyautomated
Copy link
Author

emmanuellyautomated commented Aug 30, 2016

Here's a dictionary to test it with:

dictionary = {
   "winter": {
     "Christmas": ["lights", "tree"],
     "New Years": "champagne glasses"
   },
   "summer": {
     "July Fourth": ["BBQ", "flags"]
   },  
   "spring": {
     "Memorial Day": "BBQ"
   },
   "fall": {
     "Labor Day": {
       "hot dogs":
       ['kosher', 'Nathan\'s']
     },    
     "holidays": {
       "scary": {
         "name": 'Halloween'
       },
       "not-scary": {
           "name": 'Thanksgiving',
           "games": 'bobbing'
       },
     }   
   }         
 }

@emmanuellyautomated
Copy link
Author

should work for any value: even 'kosher' and 'Thanksgiving'.

@emmanuellyautomated
Copy link
Author

doesn't quite work for values that are arrays of dictionaries

@emmanuellyautomated
Copy link
Author

emmanuellyautomated commented Aug 31, 2016

Need to make it work for dictionaries like this one:

dictionary = {
  "winter": {
    "Christmas": ["lights", "tree"],
    "New Years": "champagne glasses",
    "toys": [
      {
        "name":   'Barbie',
        "type":   'doll',
        "serial": '1111'
      },
      {
        "name":   'G.I. Joe',
        "type":   'action figure',
        "serial": '2222'
      },
      {
        "name":   'Huffy',
        "type":   'bicycle',
        "serial": '3333'
      },
    ]
  },
  "summer": {
    "July Fourth": ["BBQ", "flags"]
  },
  "spring": {
    "Memorial Day": "BBQ"
  },
  "fall": {
      "Labor Day": {
      "hot dogs": ['kosher', 'Nathan\'s']
    },
    "holidays": {
      "scary": {
        "name": 'Halloween'
      },
      "not-scary": {
        "name": 'Thanksgiving',
        "games": 'bobbing'
      },
    }
  }
}

@emmanuellyautomated
Copy link
Author

current update is not the most efficient, but it works for the structure above ^^^

@emmanuellyautomated
Copy link
Author

...now for a 'monkey wrench'...

dictionary = {
  "winter": {
    "Christmas": ["lights", "tree"],
    "New Years": "champagne glasses",
    "toys": [
      {
        "name":   'Barbie',
        "type":   'doll',
        "serial": '1111'
      },
      {
        "name":   'G.I. Joe',
        "type":   'action figure',
        "serial": '2222'
      },
      {
        "name":   'Huffy',
        "type":   'bicycle',
        "serial": '3333'
      },
      'monkey wrench'
    ]
  },
  "summer": {
    "July Fourth": ["BBQ", "flags"]
  },
  "spring": {
    "Memorial Day": "BBQ"
  },
  "fall": {
      "Labor Day": {
      "hot dogs": ['kosher', 'Nathan\'s']
    },
    "holidays": {
      "scary": {
        "name": 'Halloween'
      },
      "not-scary": {
        "name": 'Thanksgiving',
        "games": 'bobbing'
      },
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment