如何获取行和列中的字符? [英] How to get the characters in row and column?

查看:65
本文介绍了如何获取行和列中的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取二维数组中的单词,所以我需要进行行和列检查,以便获得字符.

I'm trying to extract the words in a 2d array so I need to do a row and column inspection so that I could get the characters.

示例: 我有一个包含二维的数组,此照片中显示了单击此处,然后这是存储数组的代码.我已经将其调整为15x15

Example: I have an array that contains a 2d dimensional which is shown in this photo Click here and this is the code that stores the array. I already resize it into 15x15

arr2 = np.array([my_list2])
arr2 = arr2.reshape(15,15)

问题是,每次我提取字符时,它都不会给我苹果的a. E A G L E P P L E

The problem is every time I extracted the characters it won't give me the a for the apple. E A G L E P P L E

这是让我提取字符串的代码: board_size = 15 print(arr2)

This is the code that let me extract the strings: board_size = 15 print(arr2)

for i in range(board_size):
    for j in range(board_size):
        val = arr2[i,j]
        if val != '0' :
        print(val)

` 我需要的输出是能够显示鹰和苹果.

` The output I need is to be able to display eagle and apple.

推荐答案

这是一种无需numpy即可实现的目标的方法:

This is a way to achieve what you want to do without numpy:

def print_non_0_len_ge_1(li):
    """removes 0 from front/back of line, prints rest of line if > 1 consecutive letter
    splitting at 0 between words."""
    for line in li:
        no_zero = ''.join(line).strip("0").split("0")
        for p in no_zero:
            if len(p)>1:
                print(*p,sep="\n")
                print("")   

data = [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 
        ['0', '0', '0', 'E', 'A', 'G', 'L', 'E', '0', '0'], 
        ['0', '0', '0', '0', 'P', '0', '0', '0', '0', '0'], 
        ['0', '0', '0', '0', 'P', '0', 'P', '0', '0', '0'],
        ['0', '0', '0', '0', 'L', '0', 'I', '0', '0', '0'], 
        ['0', '0', '0', 'C', 'E', 'R', 'E', 'A', 'L', '0'],
        ['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']]

# apply to lines
print_non_0_len_ge_1(data)

# apply to transposed data to get the columns
print_non_0_len_ge_1(zip(*data))  

输出:

E
A
G
L
E

C
E
R
E
A
L

A
P
P
L
E

P
I
E


如果使用numpy,则可以类似地解决它-只需删除开始/结尾0,在0处拆分并应用于法线和转置数据即可.


You can solve it similarily if using numpy - just remove the starting / ending 0, split at 0 and apply to normal and transposed data.

该方法有一个缺点-您需要在两个方向上的任何非单词形式字符之间使用0,以使其起作用(您不能使用"EGG"开头的广告(E)agle",因为您得到了GP两次.

The method has a drawback - you need 0 between any non-word-forming characters in both directions to allow it to work (you can not use "EGG" starting ad "(E)agle" because you get GP twice from it.

这篇关于如何获取行和列中的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆