Python 如何创建空数组并附加它?
我是编程新手。 我正在尝试运行此代码Python 如何创建空数组并附加它?,python,arrays,numpy,append,Python,Arrays,Numpy,Append,我是编程新手。 我正在尝试运行此代码 from numpy import * x = empty((2, 2), int) x = append(x, array([1, 2]), axis=0) x = append(x, array([3, 5]), axis=0) print(x) 但是我得到了这个错误 Traceback (most recent call last): File "/home/samip/PycharmProjects/MyCode/test.py", l
from numpy import *
x = empty((2, 2), int)
x = append(x, array([1, 2]), axis=0)
x = append(x, array([3, 5]), axis=0)
print(x)
但是我得到了这个错误
Traceback (most recent call last):
File "/home/samip/PycharmProjects/MyCode/test.py", line 3, in <module>
x = append(x, array([1, 2]), axis=0)
File "<__array_function__ internals>", line 5, in append
File "/usr/lib/python3/dist-packages/numpy/lib/function_base.py", line 4700, in append
return concatenate((arr, values), axis=axis)
File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
您可以通过[]创建空列表。要添加新项目,请使用append。要添加其他列表,请使用扩展
您可以通过[]创建空列表。要添加新项目,请使用append。要添加其他列表,请使用扩展
问题是,行x=empty2,2,int正在创建一个二维数组 稍后,当您尝试附加数组[1,2]时,会出现错误,因为它是1D数组 您可以尝试下面的代码
from numpy import *
x = empty((2, 2), int)
x = append(x,[1,2])
print(x)
问题是,行x=empty2,2,int正在创建一个二维数组 稍后,当您尝试附加数组[1,2]时,会出现错误,因为它是1D数组 您可以尝试下面的代码
from numpy import *
x = empty((2, 2), int)
x = append(x,[1,2])
print(x)
我怀疑您正在尝试复制此工作列表代码:
In [56]: x = []
In [57]: x.append([1,2])
In [58]: x
Out[58]: [[1, 2]]
In [59]: np.array(x)
Out[59]: array([[1, 2]])
但对于阵列:
In [53]: x = np.empty((2,2),int)
In [54]: x
Out[54]:
array([[73096208, 10273248],
[ 2, -1]])
尽管名称不同,np.empty数组并不是空列表的结尾。它有4个元素,即您指定的形状
In [55]: np.append(x, np.array([1,2]), axis=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-55-64dd8e7900e3> in <module>
----> 1 np.append(x, np.array([1,2]), axis=0)
<__array_function__ internals> in append(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in append(arr, values, axis)
4691 values = ravel(values)
4692 axis = arr.ndim-1
-> 4693 return concatenate((arr, values), axis=axis)
4694
4695
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
请注意,np.append已将任务传递给np.concatenate。使用axis参数,这就是这个append所做的一切。它不是一个列表附加克隆
np.concatenate要求其输入维度的一致性。一个是2,2,另一个是2,。尺寸不匹配
np.append是一个危险的函数,即使使用正确也没有那么有用。np.concatenate和各种堆栈函数都很有用。但是你需要注意形状。不要反复使用它们。列表附加在这方面更有效
出现此错误时,是否查找了np.append、np.empty和np.concatenate函数?阅读并理解文档?从长远来看,问题不能代替阅读文档。我怀疑您试图复制此工作列表代码:
In [56]: x = []
In [57]: x.append([1,2])
In [58]: x
Out[58]: [[1, 2]]
In [59]: np.array(x)
Out[59]: array([[1, 2]])
但对于阵列:
In [53]: x = np.empty((2,2),int)
In [54]: x
Out[54]:
array([[73096208, 10273248],
[ 2, -1]])
尽管名称不同,np.empty数组并不是空列表的结尾。它有4个元素,即您指定的形状
In [55]: np.append(x, np.array([1,2]), axis=0)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-55-64dd8e7900e3> in <module>
----> 1 np.append(x, np.array([1,2]), axis=0)
<__array_function__ internals> in append(*args, **kwargs)
/usr/local/lib/python3.6/dist-packages/numpy/lib/function_base.py in append(arr, values, axis)
4691 values = ravel(values)
4692 axis = arr.ndim-1
-> 4693 return concatenate((arr, values), axis=axis)
4694
4695
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
请注意,np.append已将任务传递给np.concatenate。使用axis参数,这就是这个append所做的一切。它不是一个列表附加克隆
np.concatenate要求其输入维度的一致性。一个是2,2,另一个是2,。尺寸不匹配
np.append是一个危险的函数,即使使用正确也没有那么有用。np.concatenate和各种堆栈函数都很有用。但是你需要注意形状。不要反复使用它们。列表附加在这方面更有效
出现此错误时,是否查找了np.append、np.empty和np.concatenate函数?阅读并理解文档?从长远来看,问题不能代替阅读文档。正如您在错误中看到的,您的两个数组必须匹配相同的形状,x.shape返回2,2,数组[1,2]。shape返回2,因此您需要做的是
x = np.append(x, np.array([1,2]).reshape((1,2)), axis=0)
打印x返回:
array([[1.966937e-316, 4.031792e-313],
[0.000000e+000, 4.940656e-324],
[1.000000e+000, 2.000000e+000]])
正如您在错误中看到的,您的两个数组必须匹配相同的形状,x.shape返回2,2,而数组[1,2]。shape返回2,因此您需要做的是
x = np.append(x, np.array([1,2]).reshape((1,2)), axis=0)
打印x返回:
array([[1.966937e-316, 4.031792e-313],
[0.000000e+000, 4.940656e-324],
[1.000000e+000, 2.000000e+000]])
x=np.appendx,np.array[1,2]。当我从numpy import运行此命令时,重塑1,2,axis=0*x=empty2,2,int x=appendx,array[1,2]。重塑1,2,axis=0 x=appendx,array[3,5]。重塑1,2,axis=0 printx我得到了这个结果[[30945408 0][16][12][35]]我想要的只是第三行和第四行[1 2][3 5]]请帮助mex=np.appendx,np.array[1,2]。当我从numpy import运行此命令时*x=empty2,2,int x=appendx,array[1,2]。重塑1,2,axis=0 x=appendx,array[3,5]。重塑1,2,axis=0 printx我得到这个结果[[30945408][16 16 16][1 2][3 5]]我只想要第三行和第四行[1 2][3 5]]请帮助我想要2d值我得到这个[22493056 0 16 1 2]我不想要22493056,我想要2d值,我得到这个[22493056,016,12],我不想要22493056,016