如何获得每个字母的协调并提取一个numpy数组中的字母?

时间:2019-02-01 13:38:45

标签: python

我正在拼字游戏中做一个项目,我想知道如何提取字母和起始字母及其结尾字母的坐标。

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']]

我有一个拼字游戏的输出。我如何在苹果中的老鹰的开始字母和结束字母之间进行协调。我也想把字母变成单词。总而言之,我想获取一个单词eagle中所有字母的坐标。

有人可以帮我吗,自从我学习python以来只有一个月了。谢谢。

这是原始数据。

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']

2 个答案:

答案 0 :(得分:0)

输入

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']

以下代码解决了该问题。步骤是:

  1. 您想遍历每个职位,看看它是否是首字母。
  2. 如果是,请上下检查以查看是否有单词。
  3. 检查时,请填写这些单词中字母的位置
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)

此代码打印

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

使用列表:

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)

答案 1 :(得分:0)

这样吧:

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)]}