[英]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: 这些步骤是:
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.