如何在Python中垂直比较多维列表?

如何在Python中垂直比较多维列表?,python,python-3.x,Python,Python 3.x,我有一个如下所示的网格: [['A','B','C','D','E','F','G','H'], ['-','-','-','-','-','-','-','-','-'], [0,1,0,0,1,1,0,1,'|1'], [1,1,0,1,0,0,1,0,'|2'], [1,0,1,0,0,1,1,0,'|3'], [0,0,1,0,1,1,0,1,'|4'], [0,1,0,1,1,0,1,0,'|5'], [1,0,0,1,0,1,0,1,'|6'], [0,1,1,0,1

我有一个如下所示的网格:

[['A','B','C','D','E','F','G','H'],
 ['-','-','-','-','-','-','-','-','-'],
 [0,1,0,0,1,1,0,1,'|1'],
 [1,1,0,1,0,0,1,0,'|2'],
 [1,0,1,0,0,1,1,0,'|3'],
 [0,0,1,0,1,1,0,1,'|4'],
 [0,1,0,1,1,0,1,0,'|5'],
 [1,0,0,1,0,1,0,1,'|6'],
 [0,1,1,0,1,0,0,1,'|7'],
 [1,0,1,1,0,0,1,0,'|8']]
我试图实现的是垂直比较一行中的单元格和它上面或下面的下一行。其思想是,该程序不应接受超过2个1或0,或高于或低于对方

下面的代码是我目前自己编写的,但它并不总是有效的,而且绝对没有效率。它所做的是比较所选列上的输入,并将其与下两行进行比较

positie = 0
while positie <= 7 and horizontaal == False:
    if puzzel[2][kolom] == puzzel[3][kolom] and puzzel[3][kolom] == puzzel[4][kolom]:
        positie = positie + 1
        print('You entered an invalid input.')
        print('Note: 1's and 0's cannot appear more then twice vertically and horizontally')
        puzzel[rij][kolom] = puzzelbackup[rij][kolom]
        break
    elif puzzel[3][kolom] == puzzel[4][kolom] and puzzel[4][kolom] == puzzel[5][kolom]:
        positie = positie + 1
        print('You entered an invalid input.')
        print('Note: 1's and 0's cannot appear more then twice vertically and horizontally')
        puzzel[rij][kolom] = puzzelbackup[rij][kolom]
        break
    elif puzzel[4][kolom] == puzzel[5][kolom] and puzzel[5][kolom] == puzzel[6][kolom]:
        positie = positie + 1
        print('You entered an invalid input.')
        print('Note: 1's and 0's cannot appear more then twice vertically and horizontally')
        puzzel[rij][kolom] = puzzelbackup[rij][kolom]
        break
    else:
        positie = positie + 1

你要求拼图不能接受超过2个1或0,这与你说永远不应该有3个或更多的0或1的序列是一样的

这意味着您可以遍历包含三个项的块的行,并检查它们是否都相同

有一种方法可以做到这一点:

# simplify the grid by making all items strings
puzzle = [
    ['A','B','C','D','E','F','G','H'],
    ['-','-','-','-','-','-','-','-','-'],
    ['.','1','.','.','1','1','1','0','|1'],
    ['1','.','.','1','0','0','.','0','|2'],
    ['1','0','.','0','1','1','.','0','|3'],
    ['0','0','1','.','1','1','.','1','|4'],
    ['0','0','0','1','1','0','0','0','|5'],
    ['1','0','0','0','0','.','0','.','|6'],
    ['1','1','0','.','.','.','.','.','|7'],
    ['1','0','1','1','1','.','1','0','|8'],
    ]

for row in puzzle:
    print(' '.join(row))

print()

for column in range(len(puzzle[0])):
    if column >= 2:
        # check rows, starting at the third column of the grid
        for row in range(2, len(puzzle)):
            # take three items and put them in a set
            block = set(puzzle[row][column - 2: column + 1])
            # check that the row block is valid and complete
            if len(block) == 1 and '.' not in block:
                print('ERROR: row %s, columns %s-%s' % (
                    row - 1, puzzle[0][column - 2], puzzle[0][column]))
    # check columns, starting at the third row of the grid
    for row in range(4, len(puzzle)):
        # take three items and put them in a set
        block = {puzzle[row - 2][column],
                 puzzle[row - 1][column],
                 puzzle[row][column]}
        # check that the column block is valid and complete
        if len(block) == 1 and '.' not in block:
            print('ERROR: column %s, rows %s-%s' % (
                puzzle[0][column], row - 3, row - 1))

不是最有效的解决方案,但它有效、简短且可读

def checkpuzzle(puzzle):
    """
       return True if puzzle does not have 111 or 000 either vertically or horizontally,
       False otherwise.
    """
    def checkline(line):  # False if line has 111 or 000 in it.
        line = ''.join(map(str, line)) # strings are easy to search for patterns in 
        return line.find('111') < 0 and line.find('000') < 0
    # check all rows, and then all columns if necessary
    # zip(*puzzle) is the transpose of the puzzle (ie. row becomes columns)
    return all(map(checkline, puzzle)) and all(map(checkline, zip(*puzzle)))

您好,您可能想更清楚地了解所需的程序行为,可能需要一些有效和无效网格的简单示例?@YS-L您好。我正在创建一个二进制拼图,玩家将看到一个半空的网格。和数独的想法差不多。玩家可以根据玩家想要输入的内容选择位置,例如2RowaColumn0或1。游戏规则是,必须用1和0填充网格,但水平或垂直方向上不能超过2个1或0。现在横向问题并不是一个需要解决的大问题,但我已经尝试解决纵向问题好几个小时了。我建议创建一个类来定义网格,然后使用类方法来完成任务comparison@wnnmaw你能举个例子吗?一般来说,我对python非常陌生,对类也不熟悉。您好,您的代码对我帮助很大。目前我唯一的问题是它没有检查第一行。我怎样才能使它也检测到从第一行到第三行的错误?而且,我的水平检查不再起作用了。它看起来是这样的:如果strpuzzel中的0,0,0或strpuzzel中的1,1,1,那么horizontaal=False:print'Je hebt een verkeerde invoier gegeven.'print'Let op:er mogen niet meer and 2 dezelfde cijfers verticaal voorkomen'puzzel[rij][kolom]=puzzelbackup[rij][kolom]水平面=True@Ziplay. 我已经扩展了我的解决方案来检查行和列。我不知道你的第一条评论是什么意思:它会检查第一到第三行。循环计数器从第三行开始,但块始终从第三行之前的两个位置开始,即从计数器-2到计数器。不管怎样,你可以从信息中看到所有的错误都被发现了。我爱你。在经过一些编辑以适合我的程序后,效果非常好。
def checkpuzzle(puzzle):
    """
       return True if puzzle does not have 111 or 000 either vertically or horizontally,
       False otherwise.
    """
    def checkline(line):  # False if line has 111 or 000 in it.
        line = ''.join(map(str, line)) # strings are easy to search for patterns in 
        return line.find('111') < 0 and line.find('000') < 0
    # check all rows, and then all columns if necessary
    # zip(*puzzle) is the transpose of the puzzle (ie. row becomes columns)
    return all(map(checkline, puzzle)) and all(map(checkline, zip(*puzzle)))