0

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?

martineau
  • 99,260
  • 22
  • 139
  • 249
user1362058
  • 713
  • 1
  • 5
  • 12
  • Interesting... http://stackoverflow.com/questions/41007660/python-specific-dynamic-nested-dictionaries-autovivification-implementation Any relation? – Iluvatar Dec 07 '16 at 01:14
  • Maybe. There are a few of us trying to see who can write a program to solve one of those word trek, wordbrain type games. – user1362058 Dec 07 '16 at 01:27
  • Thanks for the link @martineau, I looked through the search results on SO and I did not see that one. There were quite a few that were just no help at all, so I may have accidentally skipped that one. – user1362058 Dec 07 '16 at 01:29

0 Answers0