对矩阵的行和列操作导致代码重复

0 投票
3 回答
1065 浏览
提问于 2025-04-16 00:02

我有以下这段(Python)代码,用来检查是否有任何行或列包含相同的值:

    # Test rows ->  

        # Check each row for a win
        for i in range(self.height):                    # For each row ...

            firstValue = None                           # Initialize first value placeholder

            for j in range(self.width):                 # For each value in the row
                if (j == 0):                                # If it's the first value ...
                    firstValue = b[i][j]                        # Remember it
                else:                                       # Otherwise ...
                    if b[i][j] != firstValue:                   # If this is not the same as the first value ...
                        firstValue = None                           # Reset first value
                        break                                       # Stop checking this row, there's no win here

            if (firstValue != None):                    # If first value has been set
                                                            # First value placeholder now holds the winning player's code
                return firstValue                           # Return it

    # Test columns ->

        # Check each column for a win
        for i in range(self.width):                 # For each column ...

            firstValue = None                           # Initialize first value placeholder

            for j in range(self.height):                # For each value in the column
                if (j == 0):                                # If it's the first value ...
                    firstValue = b[j][i]                        # Remember it
                else:                                       # Otherwise ...
                    if b[j][i] != firstValue:                   # If this is not the same as the first value ...
                        firstValue = None                           # Reset first value
                        break                                       # Stop checking this column, there's no win here

            if (firstValue != None):                    # If first value has been set
                                                            # First value placeholder now holds the winning player's code
                return firstValue                           # Return it

很明显,这里有很多重复的代码。 我该如何优化这段代码呢?

谢谢!

3 个回答

1
def test_values(self, first, second, HeightWidth):
    for i in range(first):
        firstValue = None
        for j in range(second):
            (firstDimension, secondDimension) = (i, j) if HeightWidth else (j, i)
            if secondDimension == 0:
                firstValue = b[firstDimension][secondDimension]
            else:
                if b[firstDimension][secondDimension] != firstValue:
                    firstValue = None
                    break
    return firstValue

firstValue = test_values(self, self.height, self.width, true)
if firstValue:
    return firstValue

firstValue test_values(self, self.width, self.height, false)
if firstValue:
    return firstValue

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

2

要检查一行中的所有元素是否相等,我建议你可以先把这一行的元素放进一个python的 set 里,然后看看这个集合里是否只有一个元素。对列也是一样的做法。

比如这样:

def testRowWin(b):
    for row in b:
        if len(set(row)) == 1:
            return True
    return False

def testColWin(b):
    return testRowWin(zip(*b))
2

一般来说,当你想要重构代码时,可以把相似的代码片段提取成函数。这样你就可以有一个函数来检查所有单元格,看看某个索引(行或列)是否相同,另外再有一个函数来对所有列(或行)调用这个函数。不过,正如Pär在你问题的评论中提到的,如果你能提供一些你尝试过的内容,那会更容易帮助你。

但是……还有一个稍微相关的事情是,你的代码没有利用Python的函数特性。这没关系,但你要知道,像这种需要检查数组(其实是列表)中多个不同元素的任务,通常用函数式编程写会简洁得多。例如,你的例子可以这样写:

f = lambda x,y: x if x == y else False
# for Python <= 2.4 use this instead:
# f = lambda x,y: x == y and x or False
# test rows
[reduce(f,r) for r in array]
# test columns
reduce(lambda r,s: map(f,r,s), array)

不过,如果你想理解代码是怎么工作的,这样的写法可能就不太有帮助了。

撰写回答