小编haq*_*haq的帖子

Python numpy 2D数组索引

我对python和numpy很新.有人可以帮我理解如何对一些用作索引的数组进行索引.我有以下六个这样的2D阵列 -

array([[2, 0],
   [3, 0],
   [3, 1],
   [5, 0],
   [5, 1],
   [5, 2]])
Run Code Online (Sandbox Code Playgroud)

我想使用这些数组作为索引,并将值10放在新空矩阵的相应索引中.输出应如下所示 -

array([[ 0,  0,  0],
   [ 0,  0,  0],
   [10,  0,  0],
   [10, 10,  0],
   [ 0,  0,  0],
   [10, 10, 10]])
Run Code Online (Sandbox Code Playgroud)

到目前为止,我试过这个 -

    from numpy import*
    a = array([[2,0],[3,0],[3,1],[5,0],[5,1],[5,2]])
    b = zeros((6,3),dtype ='int32')
    b[a] = 10
Run Code Online (Sandbox Code Playgroud)

但这给了我错误的输出.任何帮助请.

python numpy

29
推荐指数
1
解决办法
7万
查看次数

标签 统计

numpy ×1

python ×1