如何创建列表的numpy的阵列? [英] How to create a numpy array of lists?

查看:111
本文介绍了如何创建列表的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 是诱人的,但有问题:

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)

原来,填写把所有插槽都相同的列表,因此修改一个修改所有其他人。

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]]

草签的正确方法空 A 是一个迭代的,例如

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]]]

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

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