简体   繁体   English

Python检查空字符串的2d列表?

[英]Python Check a 2d list for empty strings?

I've been trying to figure out this problem for multiple hours and still no luck. 我一直试图找出这个问题多个小时仍然没有运气。 I'm in the process of writing Connect4 in Python for a school assignment and I need a function that checks if the board is full. 我正在用Python编写Connect4用于学校作业,我需要一个检查电路板是否已满的功能。

Here is my init function 这是我的初始化函数

    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] 

My repr function 我的repr功能

    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

And what I have for my isFull function 而我的isFull功能

    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

I want to check and see if there this ' ' (a space) within the data list. 我想检查并查看数据列表中是否有这个''(空格)。 At least I think that's my problem, I'm not experienced in python so I may be misreading my issue. 至少我认为这是我的问题,我没有python的经验,所以我可能会误解我的问题。 If anyone has any ideas I'm glad to listen. 如果有人有任何想法,我很高兴听。

So if there is any space, it means the board is not full? 所以,如果有空间,这意味着电路板没有满?

Various versions: 各种版本:

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

# combine the last two
def is_full(self):  # python functions/methods are usually lower case
    for row in self.data:  # no need to index everything like c
        if any(cell == ' ' for cell in row):  # any/all are convenient testers
            return False  # if you find even one, it's done.
    return True  # if you couldn't disqualify it, then it looks full

# one line, not especially readable
def is_full(self):
    return not any(cell == ' ' for row in d for cell in row)

Your logic for isFull method is incorrect. 你的isFull方法的逻辑是不正确的。

In your current code, you are returning True from isFull as soon as you found a non-empty cell. 在当前代码中,一旦找到非空单元格,就会从isFull返回True That is incorrect. 那是不对的。 You should do the opposite. 你应该做相反的事情。

You should be doing what kobejohn had posted earlier: to return False as soon as you found an empty cell. 您应该执行kobejohn之前发布的内容:一旦找到空单元格,就返回False

And in Python you should work without indices if possible, and use Python natural looping, like in the code kobejohn had posted. 如果可能的话,在Python中你应该没有索引,并使用Python自然循环,就像kobejohn发布的代码一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM