如何创建一个 numpy 列表数组? [英] How to create a numpy array of lists?

查看:35
本文介绍了如何创建一个 numpy 列表数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 numpy 数组,其中每个元素都必须是一个列表,以便稍后我可以向每个元素附加新元素.

I want to create a numpy array in which each element must be a list, so later I can append new elements to each.

我已经在谷歌和这里查看了堆栈溢出,但似乎无处可寻.

I have looked on google and here on stack overflow already, yet it seems nowhere to be found.

主要问题是 numpy 假设您的列表必须成为一个数组,但这不是我要找的.

Main issue is that numpy assumes your list must become an array, but that is not what I am looking for.

推荐答案

正如你所发现的,np.array 在给定类似的东西时尝试创建一个二维数组

As you discovered, np.array tries to create a 2d array when given something like

 A = np.array([[1,2],[3,4]],dtype=object)

您已经应用了一些技巧来绕过这种默认行为.

You have apply some tricks to get around this default behavior.

一个是使子列表的长度可变.它不能从这些组成一个二维数组,所以它求助于对象数组:

One is to make the sublists variable in length. It can't make a 2d array from these, so it resorts to the object array:

In [43]: A=np.array([[1,2],[],[1,2,3,4]])
In [44]: A
Out[44]: array([[1, 2], [], [1, 2, 3, 4]], dtype=object)

然后您可以将值附加到每个列表中:

And you can then append values to each of those lists:

In [45]: for i in A: i.append(34)
In [46]: A
Out[46]: array([[1, 2, 34], [34], [1, 2, 3, 4, 34]], dtype=object)

np.empty 也创建了一个对象数组:

np.empty also creates an object array:

In [47]: A=np.empty((3,),dtype=object)
In [48]: A
Out[48]: array([None, None, None], dtype=object)

但是您必须小心如何将元素更改为列表.np.fill 很诱人,但有问题:

But you then have to be careful how you change the elements to lists. np.fill is tempting, but has problems:

In [49]: A.fill([])
In [50]: A
Out[50]: array([[], [], []], dtype=object)
In [51]: for i in A: i.append(34)
In [52]: A
Out[52]: array([[34, 34, 34], [34, 34, 34], [34, 34, 34]], dtype=object)

事实证明 fill 将相同的列表放在所有插槽中,因此修改一个会修改所有其他的列表.您可以使用列表列表遇到同样的问题:

It turns out that fill puts the same list in all slots, so modifying one modifies all the others. You can get the same problem with a list of lists:

In [53]: B=[[]]*3
In [54]: B
Out[54]: [[], [], []]
In [55]: for i in B: i.append(34)
In [56]: B
Out[56]: [[34, 34, 34], [34, 34, 34], [34, 34, 34]]

初始化 empty A 的正确方法是迭代,例如

The proper way to initial the empty A is with an iteration, e.g.

In [65]: A=np.empty((3,),dtype=object)
In [66]: for i,v in enumerate(A): A[i]=[v,i]
In [67]: A
Out[67]: array([[None, 0], [None, 1], [None, 2]], dtype=object)
In [68]: for v in A: v.append(34)
In [69]: A
Out[69]: array([[None, 0, 34], [None, 1, 34], [None, 2, 34]], dtype=object)

从问题和评论中有点不清楚您是要附加到列表还是将列表附加到数组.我刚刚演示了附加到列表.

It's a little unclear from the question and comments whether you want to append to the lists, or append lists to the array. I've just demonstrated appending to the lists.

有一个 np.append 函数,新用户经常误用它.它不能替代列表附加.它是 np.concatenate 的前端.这不是就地操作;它返回一个新数组.

There is an np.append function, which new users often misuse. It isn't a substitute for list append. It is a front end to np.concatenate. It is not an in-place operation; it returns a new array.

还要定义一个列表来添加它可能很棘手:

Also defining a list to add with it can be tricky:

In [72]: np.append(A,[[1,23]])
Out[72]: array([[None, 0, 34], [None, 1, 34], [None, 2, 34], 1, 23],     dtype=object)

您需要构造另一个对象数组以连接到原始对象,例如

You need to construct another object array to concatenate to the original, e.g.

In [76]: np.append(A,np.empty((1,),dtype=object))
Out[76]: array([[None, 0, 34], [None, 1, 34], [None, 2, 34], None], dtype=object)

在所有这些中,一个列表数组比一个列表列表更难构建,并且更容易或更快地操作.您必须使其成为列表的二维数组才能获得一些好处.

In all of this, an array of lists is harder to construct than a list of lists, and no easier, or faster, to manipulate. You have to make it a 2d array of lists to derive some benefit.

In [78]: A[:,None]
Out[78]: 
array([[[None, 0, 34]],
       [[None, 1, 34]],
       [[None, 2, 34]]], dtype=object)

您可以对对象数组进行整形、转置等操作,而创建和操作列表列表会变得更加复杂.

You can reshape, transpose, etc an object array, where as creating and manipulating a list of lists of lists gets more complicated.

In [79]: A[:,None].tolist()
Out[79]: [[[None, 0, 34]], [[None, 1, 34]], [[None, 2, 34]]]

===

https://stackoverflow.com/a/57364472/901925所示,np.frompyfunc 是创建对象数组的好工具.

As shown in https://stackoverflow.com/a/57364472/901925, np.frompyfunc is a good tool for creating an array of objects.

np.frompyfunc(list, 0, 1)(np.empty((3,2), dtype=object))  

这篇关于如何创建一个 numpy 列表数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆