This question already has an answer here:
这个问题已经有了答案:
- Fill a column of a numpy array with another array 3 answers
- 用另一个数组3回答填充numpy数组的一列
i have a 3d array of zeros and i want to fill it with a 1d array:
我有一个0的3d数组,我想用一个一维数组来填充它:
In [136]: C = np.zeros((3,5,6),dtype=int)
In [137]: C
Out[137]:
array([[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]])
In [138]: s
Out[138]: array([10, 20, 30, 40, 50])
I want to achieve this: (without using a loop)
我想实现这个:(不使用循环)
array([[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]],
[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]],
[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]]])
by assigning s to each column of each ith element.
将s分配给第i个元素的每一列。
note I can easily get something similar:
注意,我很容易得到类似的东西:
array([[[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60]],
[[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60]],
[[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60],
[10, 20, 30, 40, 50, 60]]])
By :
由:
C[:,:,:] = s
But can't see how to assign s to j for all i and k in [i,j,k]
但是我不知道怎么把s赋给j i和k (i,j,k)
it seems numpy prioritises the last colon C[:,:,:]. is there a nice way around this?
似乎numpy把最后一个冒号C[:,:,:]排了优先级。有什么好办法吗?
2 个解决方案
#1
0
tmp = C.swapaxes(1, 2)
tmp[:] = s
C = tmp.swapaxes(1, 2)
#2
5
You could reshape s
from (5,) to (5,1):
你可以从(5,)到(5,1):
>>> C[:] = s.reshape(5,1)
>>> C
array([[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]],
[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]],
[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]]])
#1
0
tmp = C.swapaxes(1, 2)
tmp[:] = s
C = tmp.swapaxes(1, 2)
#2
5
You could reshape s
from (5,) to (5,1):
你可以从(5,)到(5,1):
>>> C[:] = s.reshape(5,1)
>>> C
array([[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]],
[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]],
[[10, 10, 10, 10, 10, 10],
[20, 20, 20, 20, 20, 20],
[30, 30, 30, 30, 30, 30],
[40, 40, 40, 40, 40, 40],
[50, 50, 50, 50, 50, 50]]])