如何在 numpy 中创建一个“接受"附加的空列表网格?网格、创建一个、列表、如何在

2023-09-08 08:44:39 作者:热情已耗尽

I am trying to use numpy.append but something goes wrong and it just doesn't make sence to me anymore. Can someone explain why I am getting an error?

>>> np.array([[], [], []]).shape
(3, 0)

>>> a=[[], [], []]
>>> a[1].append(3)
>>> a
[[], [3], []]

>>> b=np.array(a)
>>> b[0].append(3)
array([[3], [3], []], dtype=object)

This is all logical to me, yet when I try the following it stops working.

>>> c=np.array((3,0),dtype=object)
>>> c[0].append(3)
AttributeError: 'int' object has no attribute 'append'

????
>>> np.empty((3,1))[0].append(3)
AttributeError: 'numpy.ndarray' object has no attribute 'append'

>>> np.empty((3,0))[1].append(3)
AttributeError: 'numpy.ndarray' object has no attribute 'append'

>>>np.empty((6,1),dtype=object)[0].append(3)
AttributeError: 'numpy.ndarray' object has no attribute 'append'
第一章 Numpy功能介绍及应用

Solved: How to create a numpy array of lists?

解决方案

Don't just look at the shape; check the dtype, and if object, the nature of the elements

In [282]: np.array([[], [], []])
Out[282]: array([], shape=(3, 0), dtype=float64)

A 2d array of floats. np.array tries to make a multidimensional array of numbers; it's only when it can't do that it makes an object array.

In [283]: b=np.array([[],[3],[]])
In [284]: b
Out[284]: array([[], [3], []], dtype=object)

Here the 3 sublists have different size, so it can't make a 2d array; the result is an object array, where the objects are lists, and have the append method.

In [286]: c=np.array((3,0), object)
In [287]: c
Out[287]: array([3, 0], dtype=object)

This is a (2,) object array; the 2 elements are numbers. Numbers don't have an append method.

In [288]: np.empty((3,1))
Out[288]: 
array([[ 0.],
       [ 0.],
       [ 0.]])

A (3,1) array of floats. No append method for numbers or arrays.

In [289]: np.empty((3,0))
Out[289]: array([], shape=(3, 0), dtype=float64)

Another 2d array of floats

In [290]: np.empty((6,1),object)
Out[290]: 
array([[None],
       [None],
       [None],
       [None],
       [None],
       [None]], dtype=object)

2d array of dtype object. In this case they are initialized to None. Again no append method.

More on making an array of lists

dimensions of array of arrays in numpy

and

How to keep numpy from broadcasting when creating an object array of different shaped arrays

In [305]: d=np.empty((3,),object)
In [306]: d
Out[306]: array([None, None, None], dtype=object)
In [307]: d.fill([])
In [308]: d
Out[308]: array([[], [], []], dtype=object)   # array of lists
In [309]: d[0].append([1,2,3])
In [310]: d
Out[310]: array([[[1, 2, 3]], [[1, 2, 3]], [[1, 2, 3]]], dtype=object)

But oops - those lists are all the same object (pointer) :( I have to put a different list in each element. Now I can append to them individually.

In [311]: d[...]=[[],[1,2,3],[2]]
In [312]: d
Out[312]: array([[], [1, 2, 3], [2]], dtype=object)
In [313]: d[0].append([2,3])
In [314]: d
Out[314]: array([[[2, 3]], [1, 2, 3], [2]], dtype=object)

I think you have to bite the bullet and use a list to initialize an object array of lists. There isn't a short cut:

In [319]: d=np.empty((3,),object)
In [320]: d[...]=[[] for _ in range(3)]
In [321]: d
Out[321]: array([[], [], []], dtype=object)
In [323]: d
Out[323]: array([[], [3], []], dtype=object)