python的self.boardx -= 5 什么意思,Python中的`board [x,y]`和`board [x] [y]`之间有区别吗?...

I'm working through a tutorial on GeekforGeeks website and noticed that they are checking a point in an array using board[x,y], which I've never seen before. I don't think this would work, but when I run the program, everything goes as expected.

I tried running a smaller code example using their method outlined above vs the method I'm more familiar with (board[x][y]), but when I run my code, I get TypeError: list indices must be integers or slices, not tuple

My code:

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)

Their code:

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)

Can someone explain to me why board[x,y] works, and what exactly is happening? I've never seen this before except to create lists, and am not grasping it conceptually.

解决方案

They're able to do that since they're using NumPy, which won't throw an error on that.

>>> a = np.array([[1,1,1], [1,2,2], [1,2,2]])

>>> a[1,1]

2

>>> # equivalent to

>>> a = [[1,1,1], [1,2,2], [1,2,2]]

>>> a[1][1]

2

>>>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值