【发布时间】:2019-12-12 13:12:39
【问题描述】:
我正在处理 tutorial on GeekforGeeks website 并注意到他们正在使用 board[x,y] 检查数组中的一个点,这是我以前从未见过的。我认为这行不通,但是当我运行程序时,一切都按预期进行。
我尝试使用他们上面概述的方法与我更熟悉的方法 (board[x][y]) 运行一个较小的代码示例,但是当我运行我的代码时,我得到 TypeError: list indices must be integers or slices, not tuple
我的代码:
board = [[1,1,1], [1,2,2], [1,2,2]]
win = 'True'
if board[1][1] == 2:
win = 'True by normal standards'
print(win)
if board[1, 1] == 2:
win = 'True by weird standards'
print(win)
print(win)
他们的代码:
def row_win(board, player):
for x in range(len(board)):
win = True
for y in range(len(board)):
if board[x, y] != player:
win = False
continue
if win == True:
return(win)
return(win)
有人可以向我解释为什么board[x,y] 有效吗?到底发生了什么?除了创建列表之外,我以前从未见过这种情况,并且在概念上没有掌握它。
【问题讨论】:
-
如果您在交互模式下运行(
python -i),那么type(board)将显示board是什么类型(numpy.ndarray或pandas.DataFrame)。或者查看创建和初始化board的行。
标签: python arrays list numpy indexing