簡體   English   中英

如何獲得每個字母的協調並提取一個numpy數組中的字母?

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

我正在拼字游戲中做一個項目,我想知道如何提取字母和起始字母及其結尾字母的坐標。

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

給定輸入

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)

這樣吧:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM