Skip to content
Advertisement

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 ?

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement