在Python中检查单词的行和列

发布于 2024-08-10 03:38:31 字数 456 浏览 3 评论 0原文

我正在尝试创建一个检查程序来查看该单词是否在水平或垂直矩阵中。我有检查行的代码,但是检查列是否与行代码类似?

def checkRow(table, r, pos, word):
    for i in range(0, len(word)):
        if table[r][pos+i] != word[i]:
            return False
    return True

示例表如下所示:

[
  ['a','p','p','l','e','b'],
  ['u','y','c','v','a','s'],
  ['n','u','t','o','n','s'],
  ['t','n','c','v','d','b'],
  ['o','r','i','x','o','f'],
  ['e','a','t','i','n','g']
]

I am trying to create a checking program to see if the word is in a matrix horizontally or vertically. I have the code for checking the row, but would checking the column be similar to the row code?

def checkRow(table, r, pos, word):
    for i in range(0, len(word)):
        if table[r][pos+i] != word[i]:
            return False
    return True

a sample table would be like this:

[
  ['a','p','p','l','e','b'],
  ['u','y','c','v','a','s'],
  ['n','u','t','o','n','s'],
  ['t','n','c','v','d','b'],
  ['o','r','i','x','o','f'],
  ['e','a','t','i','n','g']
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

过潦 2024-08-17 03:38:31

是不是很简单就是这样:

def checkCol(table, r, pos, word):
   for i in range(0, len(word)):
       if table[r+i][pos] != word[i]:
           return False
   return True

Isn't it simple like this:

def checkCol(table, r, pos, word):
   for i in range(0, len(word)):
       if table[r+i][pos] != word[i]:
           return False
   return True
不顾 2024-08-17 03:38:31
import itertools

def checkRow(table, r, pos, word):
    return all(w==x for w, x in itertools.izip(word, table[r][pos:]))

def checkCol(table, r, pos, word):
    return all(w==x for w, x in itertools.izip(word, table[r:][pos]))

OP 表示“他们还没有了解导入”,因此他们宁愿重新发明轮子,也不愿重用标准库中的功能。一般来说,这将是一个相当荒谬的立场,但在这种情况下,它甚至还不算太糟糕:

def checkRow(table, r, pos, word):
    return all(w==x for w, x in zip(word, table[r][pos:]))

def checkCol(table, r, pos, word):
    return all(w==x for w, x in zip(word, table[r:][pos]))

我希望至少诸如 allzip 之类的内置函数是可以接受的 - - 或者OP宁愿将二进制机器语言编码为裸机以避免学习一些 Python?-)

import itertools

def checkRow(table, r, pos, word):
    return all(w==x for w, x in itertools.izip(word, table[r][pos:]))

def checkCol(table, r, pos, word):
    return all(w==x for w, x in itertools.izip(word, table[r:][pos]))

The OP indicates "they haven't learned about import yet" so they'd rather reinvent the wheel than reuse functionality in the standard library. In general, that would be a pretty absurd stance, but in this case it ain't even too bad:

def checkRow(table, r, pos, word):
    return all(w==x for w, x in zip(word, table[r][pos:]))

def checkCol(table, r, pos, word):
    return all(w==x for w, x in zip(word, table[r:][pos]))

I hope at least builtins such as all and zip are acceptable -- or would the OP rather code binary machine language down to the bare metal to avoid learning some Python?-)

风筝在阴天搁浅。 2024-08-17 03:38:31
def checkRow(table, r, pos, word):
    return word=="".join(table[r][pos:pos+len(word)])

def checkColumn(table, r, pos, word):
    return word=="".join(row[pos] for row in table[r:r+len(word)])
def checkRow(table, r, pos, word):
    return word=="".join(table[r][pos:pos+len(word)])

def checkColumn(table, r, pos, word):
    return word=="".join(row[pos] for row in table[r:r+len(word)])
伤感在游骋 2024-08-17 03:38:31
def intable(table, word):
    if any(word in ''.join(row) for row in table):          # check rows
       return True
    return any(word in ''.join(col) for col in zip(*table)) # check columns
def intable(table, word):
    if any(word in ''.join(row) for row in table):          # check rows
       return True
    return any(word in ''.join(col) for col in zip(*table)) # check columns
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文