簡體   English   中英

numpy-1d數組中3d數組中的1個字段值

[英]numpy - 1 field value in 3d array from a 1d array

我有這個問題,我正在嘗試建立3D陣列,以后需要覆蓋它。 [:,:,5]和一維數組中的值。 我的數組在numpy看起來像這樣:

3D:

[[[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]

 [[   0.  150.   10.  300.   25.    0.]
  [   1.   25.    2.   75.    7.    0.]
  [   4.    0.    0.    0.    0.    0.]
  [   5.    0.    0.    0.    0.    0.]]]

1D:

[ 1806.    1092.     150.     150.    2669.     150.     150.     150.
   310.    7181.85]

..而我想要的是:

3d[0][0][5] = 1d[0]
3d[0][1][5] = 1d[0]
3d[0][2][5] = 1d[0]
3d[0][3][5] = 1d[0]
3d[1][0][5] = 1d[1]
3d[1][1][5] = 1d[1]
3d[1][2][5] = 1d[1]
3d[1][3][5] = 1d[1]

等等。 我一直在嘗試這樣的事情:

list_product_pricegroup[:,:,5] = migrete_array[:]

沒有任何運氣,希望有人可以指引我正確的方向。

您的數組list_product_pricegroup是10x4x6,而migrete_array是10的一維向量。由於在分配之前對list_product_pricegroup數組進行了索引(5),所以它現在是10x4矩陣。 然后,您需要將migrete_array提升為大小為4x1的二維數組以進行廣播,如下所示:

list_product_pricegroup[..., 5] = migrete_array[:, None]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM