Home > database >  Rotating list of lists searching for words, only some of the words are appended
Rotating list of lists searching for words, only some of the words are appended

Time:11-22

I'm trying to get every word in a 15x15 matrix both vertically and horizontally. I get all of the words in the horizontal search. However after I flip I only get some of the words. Is there any obvious flaw I just can't see or is there a less redundant way to do this?

This is code I have currently:

words = []
def stuff(b):
    for line in b:
        word = ""
        for tile in line:
            if tile != "  ":
                word  = tile
            elif tile == "  ":
                if word != "":
                    words.append(word)
                    word = ""

stuff(board)

print_board(board)

t_board = [list(row) for row in zip(*reversed(board))]

print_board(t_board)

stuff(t_board)

print(words)

With the output, as is pretty clear, the single letters i,j,k,l gets appended into the list. However HBA and the bottom letters aren't appended.

['A', 'B', 'C', 'D', '  ', '  ', 'E', 'F', 'G', '  ', '  ', '  ', '  ', '  ', '  ']
['B', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['H', 'I', 'J', 'K', 'L', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']

['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', 'H', 'B', 'A']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', 'I', '  ', 'B']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', 'J', '  ', 'C']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', 'K', '  ', 'D']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', 'L', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', 'E']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', 'F']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', 'G']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']
['ABCD', 'EFG', 'B', 'HIJKL', 'I', 'J', 'K', 'L']

I have tried multiple numpy rotations, however these turn the "matrix" into an array of tuples which is not what I'm searching for

CodePudding user response:

It seems like you are trying to flatten data and ignore "empties". You can do this in one line.

words = [cell for row in board for cell in row if cell.strip()]

Below is the "long-form" version of above. Both just iterate over the entire board and store cells that contain more than whitespace.

words = []
for row in board:
    for cell in row:
        if cell.strip():
            words.append(cell) 
  • Related