简体   繁体   English

检查索引是否在二维列表中 [Python]

[英]Check if index is inside 2d list [Python]

I have to write a function (def) that checks if index that I write is inside the 2d list.我必须编写一个函数 (def) 来检查我编写的索引是否在 2d 列表中。 If it is it must return True, otherwise it must return False.如果是,则必须返回 True,否则必须返回 False。

def height(board):
    height=0
    for x in board:
        height+=1
    return height

def width(board):
    wid=len(board[0])
    return wid

def on_board(board, x, y):
    check=False
    x1 = int(x)
    y1 = int(y)
    for x,y in board:
        if x1 > height(board) or y1 > width(board):
            check=False
    else:
        check=True
    return check

List looks something like this:列表看起来像这样:

board = [["V1", "V1"],
         ["V2", "V2"],
         ["V3", "V3"]]

The easiest way is with try..except..else , which will attempt to access that spot on the board.最简单的方法是使用try..except..else ,它将尝试访问板上的那个位置。 If it works, it's a valid location, and the else block will be entered.如果它有效,它是一个有效的位置,并且将进入else块。 If not, it isn't, and the except block will be entered.如果不是,则不是,并且将进入except块。 As a bonus, you can also check for whether x and y are valid integers.作为奖励,您还可以检查xy是否为有效整数。 Any failure here will produce False .这里的任何失败都会产生False

def on_board(board, x, y):
    try:
        board[int(x)][int(y)]
    except (ValueError, IndexError):
        return False
    else:
        return True

Also, the height of the board is simply def height(board): return len(board) .此外,板的高度只是def height(board): return len(board) But if you only have height() and width() for the purpose of on_board() , then you don't need height() and width() at all.但是如果你只有height()width()用于on_board() ,那么你根本不需要height()width()

As determined in chat , the testing suite which is checking your program is using 0-indexed rows and columns like we are, but the first value is the column (like an x-coordinate on a graph) and the second value is the row.正如在 chat 中确定的那样,正在检查您的程序的测试套件与我们一样使用 0 索引行和列,但第一个值是列(就像图表上的 x 坐标),第二个值是行。 It also doesn't accept indices of -1 for the user to indicate the last row/column.它也不接受-1索引供用户指示最后一行/列。 Finally, it's testing the function with integers already, so there's no need to cast with int() .最后,它已经用整数测试了函数,所以不需要用int()

def on_board(board, x, y):
    return x in range(len(board[0])) and y in range(len(board))

This checks whether x is between 0 (inclusive) and the number of columns in a row (non-inclusive), and whether y is between 0 (inclusive) and the number of rows in the board (non-inclusive).这将检查x是否在0 (含)和一行中的列数(非含)之间,以及y是否在0 (含)和板中的行数(不含)之间。 It then returns the boolean value for whether both of those conditions are true.然后它返回这两个条件是否都为真的布尔值。

You can use your two methods as written, then your on_board method can be simply checking those你可以使用你写的两种方法,然后你的on_board方法可以简单地检查那些

def height(board):
    return len(board)

def width(board):
    return len(board[0])

def on_board(board, x, y):
    return x < width(board) and y < height(board)

For example例如

>>> board = [["V1", "V1"],
             ["V2", "V2"],
             ["V3", "V3"]]
>>> on_board(board, 1, 0)
True
>>> on_board(board, 3, 4)
False

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

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