如何在2D数组中存储字符串和坐标?

时间:2019-01-31 13:13:26

标签: python arrays

我正在制作一个项目,对于拍摄的每张照片,它将存储在2d数组中。这是我已将Scrabble转换为numpy数组的图片。

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' 'A' 'P' 'P' 'L' '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' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']
 ['0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0' '0']]

执行完此操作后,我尝试使用coordinate_tiles = np.argwhere(arr1 != '0')获得字母的协调,然后使用代码获取了apple这个词。如何将其存储在2d数组中,例如

temp = [['apple'], [[7, 5],
       [7, 6],
       [7, 7],
       [7, 8],
       [7, 9]]]

1 个答案:

答案 0 :(得分:1)

看起来像字典,以单词为键,以坐标为值,可以更好地跟踪单词及其在板上的坐标。

# Create empty dictionary
temp = dict()

# Add word and coordinates to dictionary
if 'apple' not in temp:
    temp['apple'] = [[7, 5],
                     [7, 6],
                     [7, 7],
                     [7, 8],
                     [7, 9]]

# Add leek to dictionary
if 'leek' not in temp:
    temp['leek'] = [[...]]

那你可以做

# Access coordinates for apple
apple_loc = temp['apple']

# Access coordinates for leek
leek_loc = temp['leek']

查找苹果或任何其他单词的坐标。