Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python检查2d列表中是否有空字符串?_Python - Fatal编程技术网

Python检查2d列表中是否有空字符串?

Python检查2d列表中是否有空字符串?,python,Python,我已经试着解决这个问题好几个小时了,但还是没有成功。我正在用Python为学校作业编写Connect4,我需要一个函数来检查黑板是否已满 这是我的init函数 def __init__( self, width, height ): self.width = width self.height = height self.data = [] # this will be the board for row in range( self.heigh

我已经试着解决这个问题好几个小时了,但还是没有成功。我正在用Python为学校作业编写Connect4,我需要一个函数来检查黑板是否已满

这是我的init函数

    def __init__( self, width, height ): 
    self.width = width 
    self.height = height 
    self.data = [] # this will be the board 

    for row in range( self.height ): 
        boardRow = [] 
        for col in range( self.width ): 
            boardRow += [' '] 
        self.data += [boardRow] 
    def __repr__(self): 
    #print out rows & cols 
    s = '' # the string to return 
    for row in range( self.height ): 
        s += '|' # add the spacer character 
        for col in range( self.width ): 
            s += self.data[row][col] + '|' 
        s += '\n' 

s += '--'*self.width + '-\n'

for col in range( self.width ):
    s += ' ' + str(col % 10)
s += '\n'

return s
Myrepr功能

    def __init__( self, width, height ): 
    self.width = width 
    self.height = height 
    self.data = [] # this will be the board 

    for row in range( self.height ): 
        boardRow = [] 
        for col in range( self.width ): 
            boardRow += [' '] 
        self.data += [boardRow] 
    def __repr__(self): 
    #print out rows & cols 
    s = '' # the string to return 
    for row in range( self.height ): 
        s += '|' # add the spacer character 
        for col in range( self.width ): 
            s += self.data[row][col] + '|' 
        s += '\n' 

s += '--'*self.width + '-\n'

for col in range( self.width ):
    s += ' ' + str(col % 10)
s += '\n'

return s
我所拥有的是我的全部功能

    def isFull(self):
# check if board is full
for row in range(0,(self.height-(self.height-1))):
    for col in range(0,self.width):
    if (' ') not in self.data[row][col]:
        return True

我想检查并查看数据列表中是否有此“”(空格)。至少我认为这是我的问题,我没有python方面的经验,所以我可能误读了我的问题。如果有人有任何想法,我很乐意倾听。

那么如果有任何空间,这意味着棋盘还没有满

各种版本:

# straightforward but deep
def is_full(self):
    for row in self.data:
        for cell in row:
            if cell == ' ':
                return False
    return True



isFull
方法的逻辑不正确

在当前代码中,一旦找到非空单元格,您将从
isFull
返回
True
。这是不正确的。你应该做相反的事

你应该像kobejohn之前发布的那样:在找到空单元格后立即返回
False


在Python中,如果可能的话,您应该在没有索引的情况下工作,并使用Python自然循环,就像kobejohn发布的代码一样。

那么,您的问题是什么?哪种方法不起作用?它怎么不起作用?请正确对齐您的代码。另外,这个
范围是什么(0,(self.height-(self.height-1))
?它与
范围(0,1)
相同。我需要一个函数,可以检查2d列表中的任何位置是否有空格“”。您可能希望在代码正常工作后(不存在重大错误)发布代码,并获得一些改进意见。我真的非常感谢您。我不是一个很好的程序员,所以我已经花了好几个小时试图弄明白这一点,而且效果很好。再次感谢你!如果你能正式接受我的答复,我将不胜感激。周围都是温暖的绒毛。