python - 用数字序列填充 numpy 数组
问题描述
我有一个 numpy 数组,其形状(6, 3, 4)
我想用数字的升序填充,因此结果数组是这样的:
array([[[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[1, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[2, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[3, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[4, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]],
[[5, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]], dtype=uint8)
如果可能的话,我不想使用循环。
我试过以下没有运气:
import numpy as np
new_row = np.zeros([6, 3, 4], dtype=np.uint8)
new_row[:,0:1,0:1] = range(6)
这导致ValueError: could not broadcast input array from shape (6) into shape (6,1,1)
任何帮助表示赞赏
解决方案
推荐阅读
- java - 有没有办法在程序仍在运行时替换 eclipse 中的输出?
- typescript - ORM 类型的横向连接
- react-redux - redux-oidc 从 keycloak 重定向 - USER_EXPIRED 在 USER_FOUND 之前调用,导致登录屏幕闪烁
- python - py2neo.database.work.ClientError: [Security.Unauthorized] 客户端因认证失败而未授权
- arrays - 如何在 Spark 中将数组项分成单独的列?
- three.js - three.js SpotLight 急性光束不起作用
- python - 在函数内部使用函数
- wpf - 在朋友 PC 上部署 WPF 应用程序
- android-studio - Android studio - 导入的项目,运行/调试配置中没有模块
- oracle - 当我修改分区表初始值时如何解决 ORA-14257