1

I need to find words in a list of lists (a somewhat matrix), by entering a given direction to search. so for example, if I need to search all words that are horizontal from right to left - I will do that by manipulating the indexes that run over the columns.

the_matrix = [['a', 'p', 'p', 'l', 'e'],
              ['a', 'g', 'o', 'd', 'o'],
              ['n', 'n', 'e', 'r', 't'],
              ['g', 'a', 'T', 'A', 'C'],
              ['m', 'i', 'c', 's', 'r'], 
              ['P', 'o', 'P', 'o', 'P']]
the_word_list = ['ert','PoP']

def find_words_in_matrix(directions):
    good_list = []
    for col in range(len(the_matrix[0])):
        for row in range(len(the_matrix)):
            for word in the_word_list:
                for i in range(len(word)):
                    found_word = True
                    #a = word[i]
                    if col + i > len(the_matrix[0])-1:
                        break
                    #b = the_matrix[row][col+i]
                    if word[i] != the_matrix[row][col+i]:
                        found_word=False
                        break
                if found_word is True:
                    good_list.append(word)

    return good_list

Im getting the output:

['PoP', 'ert', 'PoP', 'ert', 'PoP']

instead of:

['PoP', 'ert', 'PoP']

*pop shows up twich at the bottom line, not three times. and ert only once. that is my problem

thanks for the help!

  • @tobias_k added. thanks! –  Nov 19 '18 at 17:01
  • Possible duplicate of [Finding word in a matrix](https://stackoverflow.com/questions/53358280/finding-word-in-a-matrix) – iGian Nov 19 '18 at 17:10
  • @iGian, Ive checked there but My problem is rather specific and I couldnt find the answer there. –  Nov 19 '18 at 17:11
  • Then, please check this: https://stackoverflow.com/questions/53365086/adding-items-to-list-when-they-already-exist-and-when-using-for-loop --- Maybe it helps. – iGian Nov 19 '18 at 17:17
  • @tobias_k Ive added the variables. thank you –  Nov 19 '18 at 17:25
  • iGian, thank you but my problem is only with the range ;( –  Nov 19 '18 at 17:27
  • oh good thanks ! @tobias_k –  Nov 19 '18 at 17:34
  • ive updated my question. (I couldnt find any use in the provided posts) @tobias_k –  Nov 19 '18 at 17:38

1 Answers1

0

You are getting stray matches when the break terminates the loop early before the whole word has matched. To eliminate this, you can keep track of the match length:

def find_words_in_matrix(directions):
    good_list = []
    for col in range(len(the_matrix[0])):
        for row in range(len(the_matrix)):
            for word in the_word_list:
                match_len = 0
                for i in range(len(word)):
                    found_word = True
                    #a = word[i]
                    if col + i > len(the_matrix[0])-1:
                        break
                    #b = the_matrix[row][col+i]
                    if word[i] != the_matrix[row][col+i]:
                        found_word=False
                        break
                    match_len += 1
                if (match_len == len(word)) and (found_word is True):
                    good_list.append(word)

    return good_list
MassPikeMike
  • 672
  • 3
  • 12
  • thanks alot! can you please explain the purpose of the match len again? @MassPikeMike –  Nov 19 '18 at 19:00
  • 1
    @python_beginner Consider when you are matching "ert" starting at the last column of the first row. The "e" matches, so `found_word` is set to `True`. Then on the next loop `col + i` exceeds the word length, so the loop exits without trying to match the "r". But `match_len` is still only 1, so the new check prevents this partial match from being added to `good_list`. There are other ways to get the same effect, like comparing `i` to `len(word)`. – MassPikeMike Nov 19 '18 at 19:22
  • thanks! btw, if I wanted to check words in the opposite direction (from right to left), how will this affect the break parts? @MassPikeMike –  Nov 19 '18 at 19:59
  • @python_beginner hmm, depends on how you implement changing the direction. Maybe that should be a new question. – MassPikeMike Nov 19 '18 at 20:00