如何在矩阵中查找单词-Python [英] How to find words in a matrix - Python

查看:128
本文介绍了如何在矩阵中查找单词-Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提供了一个功能,可以在充满字母的矩阵中水平搜索单词.现在,我正在尝试重用此代码,以便在矩阵中查找任何单词.我需要逐行阅读矩阵,将制成的单词与我的单词列表(例如字典)进行比较,如果列表中存在该单词,则已经找到了一个单词. 这是我的初始代码:

I've made a function to search horizontally for a word in a matrix full of letters. Now I'm trying to reuse this code in order to find any word in matrix. I need to read the matrix line by line, compare the words made with my list of words (like a dictionary) and if the word exists in the list, a word has been found. This is my initial code:

def search(p):  
    x=matrix() #matrix of letters
    for line in x:
        if p in ''.join(line):
            return True

我写了一些其他代码,但是没有一个起作用.我一直在寻找类似的问题,但没有一个回答我的问题.

I've written some other code but none of it works. I've been looking for similar questions but none of them answer my question.

这是我拥有但不起作用的代码:

This is the code I have and doesn't work:

def auto_search():
    l=[] #list of words found
    x=matrix()
    for line in x:
        for i in ''.join(line):
            if search_dic(i)!=-1: #searchs in the list of words if the word is there
                l.append(i)
    print (l)

例如,一个具有以下矩阵:

For example, a have this matrix:

[[a,p,e,n],
 [g,h,j,k],
 [e,r,l,d]]

该函数必须读取矩阵并自行找到单词"pen". 任何帮助,将不胜感激.谢谢

The function has to read the matrix and find the word "pen" by itself. Any help would be appreciated. Thank you

推荐答案

通常,我建议您寻找将矩阵转置为其他矩阵的方法,以便可以在每个矩阵上重用扫描代码.例如,您要获取矩阵:

In general I'd suggest that you look for ways to transpose the matrix into other matrices such that you can re-use your scanning code on each of them. For example you want to take your matrix:

a p e n
g h j k
e r l d

(为简洁起见,此处未标点显示)...并因此进行了转置:

(rendered here without punctuation for brevity) ... and transpose it thus:

a g e
p h r
e j l
n k d

...按列.然后,您可以使用您的代码对其进行扫描.这实际上与编写新功能来向下"扫描每一列以查找目标单词相同.

... column-wise. You can then scan that with your code. This is effectively the same as writing a new function to scan each column "downwards" for your target word(s).

您还可以反转原始矩阵和此转置矩阵的每一行,以分别扫描向后"和向上".

You can also reverse each line of the original matrix and of this transposed matrix to scan "backwards" and "upwards" respectively.

我将重新编写您的 search()函数,以使用两个参数-矩阵和目标.然后,您可以使用初始矩阵和转置矩阵来调用它.我还将修改 search()函数,以向前和向后搜索每一行(并且我可能会将最后一部分设为可选).

I would re-write your search() function to take two arguments --- a matrix and a target. Then you can call it with the initial matrix, and the transposed matrix. I'd also modify the search() function to search each row forwards and backwards (and I might make that last part optional).

所以我的 search()函数看起来像这样:

So my search() function would look like this:

#!/usr/bin/python
def search(matrix, target, rev=True):
    for each in matrix:
        if target in ''.join(each):
            return True
        if rev and target in ''.join(reversed(each)):
            return True
    return False

我需要一个 transpose()函数,恰巧它很简单:

And I need a transpose() function which, as it happens, can be trivially simple:

#!/usr/bin/python
def transpose(matrix):
    return zip(*matrix)

现在我的主要代码路径如下所示:

Now my main code path would look something like:

#!/usr/bin/python
if search(matrix, target) or search(transpose(matrix), target):
    print "%s found in matrix" % target

这篇关于如何在矩阵中查找单词-Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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