pythondictionarysearchtriedata-retrieval

how do we check if a word exist inside a trie dictionary in python?


I have this trie dictionary:

{'s': {'h': {'o': {'w': {'value': 'a programm'},
                  'o': {'t': {'value':'a bullet'}},
                  'e': {'value': 's.th to wear'}},
             'a': {'m': {'e': {'value': 'a feeling'}}},
             'i': {'t': {'value': 'deficate'}}}}}

I want to define a function that searches through this trie dict and finds its values. how to do it ?


Solution

  • here is how I do it:

    def finder(dict_trie,word):
        if word:
            first,rest = word[0],word[1:]
            finder(dict_trie[first],rest)
        else:
            print(dict_trie["value"])
    
    
    
    finder(dict_trie,"shit")
    
    
    result = deficate