I am reading in a dictionary file and I want to create something like this:
Word First Second Third Fourth
Length Letter Letter Letter Letter ...
3 --> c --> a --> t
--> r
--> b
--> o --> p
--> b
--> b --> e --> t
--> g
4 --> p --> u --> n --> t
...
Hopefully you get the idea. I want to be able to test an array of data against the Dictionary like below. Obviously just a basic example, not how it will be done.
smallDictionary = dictionary[3]
found = []
if dataArr[0][0] in smallDictionary:
if dataArr[0][1] in smallDictionary:
if dataArr[0][2] in smallDictionary:
found.append(''.join([dataArr[0][0],dataArr[0][1],dataArr[0][2]])
So, basically I'm trying to create a dictionary tree and I'm not sure how the declaration should work as I read the dictionary from file.
for word in open(inputFile):
word = word.rstrip()
if len(word) not in dictionary:
dictionary[len(word)] = {}
for x in len(word):
if(x == 0):
if word[x] not in dictionary[len(word)]:
dictionary[len(word)][word[x]] = {}
At that point I realized I was stuck, but I am not sure how to fix it since each level would add dictionary[len(word)][word[x-2]][word[x-1]][word[x]]
. Also, I would have to match each x
number specifically and I don't know how many letters there will be. I guess I could just write code out to 50 but that doesn't seem like the right thing to do.
Any suggestions?