在Python中,可以使用`numpy`库的`reshape`函数将一维数组转换为二维数组。以下是如何操作的示例:
import numpy as np创建一维数组arr1d = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])使用reshape函数将一维数组转换为二维数组指定新形状为(2,5)arr2d = np.reshape(arr1d, (2, 5))print(arr2d)输出:[[ 1 2 3 4 5][ 6 7 8 9 10]]使用reshape函数将一维数组转换为二维数组指定新形状为(-1,4),自动计算新形状arr2d_auto = np.reshape(arr1d, (-1, 4))print(arr2d_auto)输出:[[ 1 2 3 4][ 5 6 7 8][ 9 10 11 12]]
请注意,`reshape`函数要求新数组的元素总数必须与原始数组相同,否则会抛出`ValueError`异常。

