代码之家  ›  专栏  ›  技术社区  ›  ramgorur

如果存在匹配维度(python/numpy),则无法创建不规则数组

  •  1
  • ramgorur  · 技术社区  · 3 年前

    >>> import numpy as np
    >>> A = np.zeros((12,2))
    >>> B = np.zeros((12,))
    >>> A
    array([[0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.]])
    >>> B
    array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
    >>> C = np.array([A, B], dtype=object)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    ValueError: could not broadcast input array from shape (12,2) into shape (12)
    

    >>> A = np.zeros((20,2))
    >>> B = np.zeros((12,))
    >>> C = np.array([A, B], dtype=object)
    >>> C
    array([array([[0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.]]),
           array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])],
          dtype=object)
    

    (12,2) (12,)

    0 回复  |  直到 3 年前