【问题标题】:How to create an empty array and append it?如何创建一个空数组并附加它?
【发布时间】: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


【解决方案1】:

您可以通过[] 创建空列表。要添加新项目,请使用append。要添加其他列表,请使用extend。

x = [1, 2, 3]
x.append(4)
x.extend([5, 6])

print(x) 
# [1, 2, 3, 4, 5, 6]

【讨论】:

    【解决方案2】:

    问题在于x = empty((2, 2), int) 行正在创建一个二维数组。

    稍后当您尝试附加 array([1, 2]) 时会收到错误,因为它是一维数组。

    你可以试试下面的代码。

    from numpy import *
    
    x = empty((2, 2), int)
    
    x = append(x,[1,2])
    
    print(x)
    
    

    【讨论】:

    • 我想要 2d 值我得到这个 [22493056 0 16 16 1 2] 我不想要 22493056 0 16 16
    【解决方案3】:

    我怀疑您正在尝试复制此工作列表代码:

    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(以及各种stack)函数很有用。但是你需要注意形状。并且不要迭代地使用它们。列表追加更有效。

    当您遇到此错误时,您是否查找过 np.append、np.empty(和 np.concatenate)函数?阅读并理解文档?从长远来看,SO 问题并不能替代阅读文档。

    【讨论】:

      【解决方案4】:

      正如您在错误中看到的,您的两个数组必须匹配相同的形状,x.shape 返回 (2,2),并且 array([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]])
      

      【讨论】:

        猜你喜欢
        • 2020-07-03
        • 2019-02-28
        • 1970-01-01
        • 2019-02-16
        • 2016-02-26
        • 1970-01-01
        • 1970-01-01
        • 2011-06-15
        • 1970-01-01
        相关资源
        最近更新 更多