简体   繁体   English

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

[英]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. 总而言之,我想获取一个单词eagle中所有字母的坐标。

Can someone help me with this, its been only a month since Im studying python. 有人可以帮我吗,自从我学习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)]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从numpy数组中的每个字符串中提取第一个字母 - Extract the first letter from each string in a numpy array 如何通过映射每个大写字母仅提取括号内首字母缩略词后的缩写 - How do i extract only abbreviation following acronyms inside the brackets by mapping each Capital letter 如何打印 python 中每个字符串的第一个字母、第二个字母和第三个字母? - How do I print the first letter and second letter and third.. of each string in python? 如何检查每个字母是否在多个字符串中? - How do I check if each letter is in multiple strings? 如何检查数组中每个项目的首字母? - How can I check the first letter of each item in an array? 如何阻止每个字母打印在不同的行上? - How do I stop each letter from printing on different line? 如何为每个大写字母分配数值? - How do I assign a numerical value to each uppercase Letter? 如何运行循环,直到我收到每个字母 - How to run loop until I get each individual letter 如何获取我(如果在列表中为“输入”)检查输入的每个字母,而不仅仅是输入的第一个字母? - How do I get my (if “input” in list) to check every letter of the input and not just the first letter of input? 如何更改网格(字符串数组)中的单个字母? - How do I change a single letter in a grid (array of strings)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM