在numpy中交替值 [英] alternating values in numpy

查看:88
本文介绍了在numpy中交替值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图使我的代码更高效和可读性强,我被困住了.假设我要构建像国际象棋棋盘这样的东西,并在8x8的网格上使用黑白交替的颜色.因此,使用numpy,我做到了:

Trying to make my code more efficient and readable and i'm stuck. Assume I want to build something like a chess board, with alternating black and white colors on an 8x8 grid. So, using numpy, I have done this:

import numpy as np
board = np.zeros((8,8), np.int32)
for ri in range(8):
    for ci in range(8):
            if (ci + ri) % 2 == 0:
                    board[ri,ci] = 1

很好的输出:

array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]], dtype=int32)

然后我可以解析为白色正方形或黑色正方形.但是,实际上,我的数组要大得多,并且这种方式效率很低并且无法读取.我以为numpy已经解决了这个问题,所以我尝试了这个:

That I can then parse as white squares or black squares. However, in practice my array is much larger, and this way is very inefficient and unreadable. I assumed numpy already has this figured out, so I tried this:

board = np.zeros(64, np.int32)
board[::2] = 1
board = board.reshape(8,8)

但是输出错误,看起来像这样:

But that output is wrong, and looks like this:

array([[1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [1, 0, 1, 0, 1, 0, 1, 0]], dtype=int32)

是否有更好的方法来实现我想要的有效工作(最好是可读的)?

Is there a better way to achieve what I want that works efficiently (and preferably, is readable)?

注意:我不附加1和0,只要可以使用其他类型的值即可,即使是True/False或2种字符串也可以轻松实现

Note: i'm not attached to 1's and 0's, this can easily be done with other types of values, even True/False or strings of 2 kinds, as long as it works

推荐答案

这是使用 slicing ,并以正确的开始和2的大小逐步进行两步操作-

Here's one approach using slicing with proper starts and stepsize of 2 in two steps -

board = np.zeros((8,8), np.int32)
board[::2,::2] = 1
board[1::2,1::2] = 1

样品运行-

In [229]: board = np.zeros((8,8), np.int32)
     ...: board[::2,::2] = 1
     ...: board[1::2,1::2] = 1
     ...: 

In [230]: board
Out[230]: 
array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]], dtype=int32)

其他棘手的方法-

1)广播比较:

In [254]: r = np.arange(8)%2

In [255]: (r[:,None] == r)*1
Out[255]: 
array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]])

2)广播添加内容:

In [279]: r = np.arange(8)

In [280]: 1-(r[:,None] + r)%2
Out[280]: 
array([[1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1],
       [1, 0, 1, 0, 1, 0, 1, 0],
       [0, 1, 0, 1, 0, 1, 0, 1]])

这篇关于在numpy中交替值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆