【发布时间】:2023-03-16 16:04:01
【问题描述】:
我是编程新手。 我正在尝试运行此代码
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)
【问题讨论】:
-
x = np.append(x, np.array([1, 2]).reshape(1,2), axis=0) -
当我从 numpy import * x = empty((2, 2), int) x = append(x, array([1, 2]).reshape((1, 2) ), axis=0) x = append(x, array([3, 5]).reshape((1, 2)), axis=0) print(x) 我得到这个结果 [[30945408 0] [ 16 16 ] [ 1 2] [ 3 5]] 我想要的只是第三行和第四行 [1 2] [ 3 5]] 请帮帮我
标签: python arrays numpy append