python - 将两个 numpy 数组按索引连接在一起
问题描述
我有两个 2D numpy 数组:
a = np.array([[0,1,2,3,4],
[0,1,2,3,4],
[0,1,2,3,4]
...
])
b = np.array([[0,0,0,0,0],
[1,1,1,1,1],
[2,2,2,2,2],
...
])
我如何能够获得一个 numpy 数组,其中每个精确索引将连接在一起?
OUT:
[[0,0], [1,0], [2,0], [3,0], [4,0] [0,1], [1,1], [2,1], [3,1], [4,1] ...]
解决方案
你想要堆栈
result = np.stack((a,b), axis=2)
array([[[0, 0],
[1, 0],
[2, 0],
[3, 0],
[4, 0]],
[[0, 1],
[1, 1],
[2, 1],
[3, 1],
[4, 1]],
[[0, 2],
[1, 2],
[2, 2],
[3, 2],
[4, 2]]])
取自@user15270287,您可以使用重塑结果np.reshape(result, (-1, 2))
推荐阅读
- java - 使用 JAVA 将 .vox 转换为 .mp3
- javascript - 可以捕获 ERR_BLOCKED_BY_CLIENT Axios 吗?
- c++ - Getting undocumented error code from recvfrom
- reactjs - 在 Heroku 上部署 react/apollo-server 应用程序
- python - 如何在 Python 中将 CSV 列作为字符串读取
- indexing - 找出列中每个字段的范围
- c# - CRM 查询是否需要在线 Asp.net 应用程序中的 Azure 凭据或令牌?
- rebus - 带有 Rebus 的可配置主题名称
- json - 如何从字符串中提取某些信息并在python中创建一个json对象
- java - 用于动态创建成员变量和 getter 方法的 Java 助手类