小编Bro*_*-AC的帖子

Python中的`board [x,y]`和`board [x] [y]`之间有区别吗?

我正在研究GeekforGeeks网站上教程,并注意到他们正在使用来检查数组中的点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)
Run Code Online (Sandbox Code Playgroud)

他们的代码:

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 == …
Run Code Online (Sandbox Code Playgroud)

python arrays indexing numpy list

46
推荐指数
5
解决办法
3180
查看次数

标签 统计

arrays ×1

indexing ×1

list ×1

numpy ×1

python ×1