首页 > 解决方案 > 将 numpy 数组从 [1,2,3,4] 重塑为 [[1],[2],[3],[4]]

问题描述

我想重塑

[1,2,3,4]

进入:

[[1],[2],[3],[4]]

有没有简单的 numpy 方法来做到这一点?

标签: pythonnumpy

解决方案


利用

reshaped_arr = arr.reshape(4,1)

测试一下:

import numpy as np

arr = np.arange(4)
reshaped_arr = arr.reshape(4,1)
print (reshaped_arr)

输出:

[[0]
 [1]
 [2]
 [3]]

推荐阅读