I'm trying to implement a nested dictionary structure in a specific manner. I'm reading in a long list of words. These words are eventually going to need to be searched through often and efficiently so this is how I want my dictionary to be set up:
I'm trying to make a nested dictionary structure where the first key value is the length of the word, the value is a dict with the key being the first letter of the word and the value is a dict with the key being the second letter of the word and the value being a dict with the key as third letter of the word etc..
so if I read in "car" "can" and "joe"
I get
{3: {c: {a: {r: car, n: can}}},j: {o: {e: joe}}}
I need to do this for about 100,000 words though and they vary in length from 2 to 27 letters.
I've looked through What is the best way to implement nested dictionaries? and Dynamic nested dictionaries.
but haven't had any luck figuring this out.
I can certainly get my words out of my text file using
for word in text_file.read().split()
and I can break into each character using
for char in word
or
for i in range(len(word)):
word[i]
I just can't figure out how to get this structure down. Any help would be greatly appreciated.