简体   繁体   中英

How do I get the coordination of each letter and also extract the letter in a numpy array?

I am making a project in the scrabble and I am wondering how can I extract the letter and the coordinates of the starting letter and its ending letter.

arr1 = [['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' 'E' 'A' 'G' 'L' 'E' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' 'P' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' 'P' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' 'L' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' 'E' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']]

I have the ouput of a scrabble game. How do I get its coordination of the starting letter in eagle also in apple and its ending letter. I also want to get the letter to form into words. In summary, I want to get the coordinates of all the letters in a word eagle.

Can someone help me with this, its been only a month since Im studying python. Thank you.

This is the orginal data.

k = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'E', 'A', 'G', 'L', 'E', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'P', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'P', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'L', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'E', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']

Given input

k = ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', 'E', 'A', 'G', 'L', 'E', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', 'P', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', 'P', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', 'L', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', 'E', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']

The following codes solves the problem. The steps are:

  1. You want to go through every position and see if it's a starting letter.
  2. If it is, check across and down to see if there are words.
  3. As you check, fill in the positions of the letters in those words
import numpy

my_board = numpy.array(k).reshape((15, 15))
out_dict = {}

for j in range(my_board.shape[1]):
    for i in range(my_board.shape[0]):
        if my_board[i][j] == '0':
            continue

        # across
        if j == 0 or my_board[i][j - 1] == '0':
            k, l = i, j
            word_across = ''
            word_across_pos = []
            while l < 15 and k < 15 and my_board[k][l] != '0':
                word_across += my_board[k][l]
                word_across_pos.append((k, l))
                l += 1

            if len(word_across) > 1:
                out_dict.setdefault(word_across, [])
                out_dict[word_across].append(word_across_pos)

        # down
        if i == 0 or my_board[i - 1][j] == '0':
            k, l = i, j
            word_down = ''
            word_down_pos = []
            while l < 15 and k < 15 and my_board[k][l] != '0':
                word_down += my_board[k][l]
                word_down_pos.append((k, l))
                k += 1

            if len(word_down) > 1:
                out_dict.setdefault(word_down, [])
                out_dict[word_down].append(word_down_pos)

print(out_dict)

This code prints

{'EAGLE': [[(7, 5), (7, 6), (7, 7), (7, 8), (7, 9)]], 'APPLE': [[(7, 6), (8, 6), (9, 6), (10, 6), (11, 6)]]}

Using list:

import numpy

my_board = numpy.array(k).reshape((15, 15))
out_list = []

for j in range(my_board.shape[1]):
    for i in range(my_board.shape[0]):
        if my_board[i][j] == '0':
            continue

        # across
        if j == 0 or my_board[i][j - 1] == '0':
            k, l = i, j
            word_across = ''
            word_across_pos = []
            while l < 15 and k < 15 and my_board[k][l] != '0':
                word_across += my_board[k][l]
                word_across_pos.append((k, l))
                l += 1

            if len(word_across) > 1:
                out_list.append([word_across, word_across_pos])

        # down
        if i == 0 or my_board[i - 1][j] == '0':
            k, l = i, j
            word_down = ''
            word_down_pos = []
            while l < 15 and k < 15 and my_board[k][l] != '0':
                word_down += my_board[k][l]
                word_down_pos.append((k, l))
                k += 1

            if len(word_down) > 1:
                out_list.append([word_down, word_down_pos])

print(out_list)

How about like this:

array([['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', 'E', 'A', 'G', 'L', 'E', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', 'P', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', 'P', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', 'L', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', 'E', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0'],
       ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '0', '0']], dtype='<U1')

def grid_mapper(grid, t=False):
    word = ''
    coords = []
    for idx, vals in enumerate(grid):
        if sum(map(str.isalpha, vals)) < 2:
            continue
        else:
            for i, v in enumerate(vals):
                if v.isalpha():
                    word += v
                    if not t:
                        coords.append((idx, i))
                    else:
                        coords.append((i, idx))
                else:
                    continue
    return word, coords

res = dict()
for g in [(a,), (a.T, True)]:
    w, c = grid_mapper(*g)
    res[w] = c

print(res)

{'EAGLE': [(7, 5), (7, 6), (7, 7), (7, 8), (7, 9)],
 'APPLE': [(7, 6), (8, 6), (9, 6), (10, 6), (11, 6)]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM