简体   繁体   English

如何在numpy中实现其值是可变长度数组的字典

[英]How can I implement a dictionary whose value is mutable length array in numpy

I want to use numpy to implement the following data structure. 我想使用numpy来实现以下数据结构。 Now I use python dictionary to do the work, but it's hard to do the vector operations, I have to add the vector many times, so I want to use numpy to simplify the work. 现在,我使用python字典来完成这项工作,但是很难执行向量操作,我必须多次添加向量,因此我想使用numpy来简化工作。 The length of hosts will vary during program execution. 主机的长度在程序执行期间会有所不同。 Is it possible for me to do this job with numpy structured arrays, notice that the length of list is mutable? 我是否可以使用numpy结构化数组来完成这项工作,请注意列表的长度是可变的? I'm not familiar with it, just want to know whether it's possible, so that it won't be a waste of time. 我不熟悉它,只是想知道是否有可能,这样就不会浪费时间。

{
  "0" :{
      "coordinates": [100, 100],
      "neighbours": [1, 40],
      "hosts":[],
      "v-capacity":20,
      "v-immature":0,
      "v-state":[20, 0, 0, 0]
  },
  "1" :{
      "coordinates": [200, 100],
      "neighbours": [0, 2, 41],
      "hosts":[],
      "v-capacity":20,
      "v-immature":0,
      "v-state":[20, 0, 0, 0]
  },

What you show is a dictionary whose values are also dictionaries. 您显示的是一本字典,其值也是字典。 Some values of the nested dictionaries are scalars, others are lists. 嵌套字典的某些值是标量,其他值是列表。 neighbors list varies in length. neighbors列表的长度有所不同。

I can picture creating a structured array with fields corresponding to the inner dictionary keys. 我可以图片创建一个结构化的数组,其中包含与内部字典键相对应的字段。

The coordinates and v-state fields could even have inner dimensions of (2,) and (4,). coordinatesv-state字段的内部尺寸甚至可以为(2,)和(4,)。

But for variable length neighbors or hosts the best we can do it define those fields as having object dtype, which will store the respective lists elsewhere in memory. 但是对于可变长度的neighborshosts ,我们可以做到最好,将那些字段定义为具有对象dtype,这会将相应的列表存储在内存中的其他位置。 Math on that kind of array is limited. 这种数组的数学是有限的。

But before you get too deep into structured arrays, explore creating a set of arrays to store this data, one row per item in the out dictionary. 但是,在您不深入了解结构化数组之前,请探索创建一组数组来存储此数据,在out字典中每个项目row

coordinates = np.array([[100,100],[200,100]])
neighbors = np.array([[1, 40],[0, 2, 41]])

Make sure you understand what those expressions produce. 确保您了解这些表达式产生的内容。

In [537]: coordinates
Out[537]: 
array([[100, 100],
       [200, 100]])
In [538]: neighbors
Out[538]: array([[1, 40], [0, 2, 41]], dtype=object)

Here's an example of a structured array that can hold these arrays: 这是可以容纳这些数组的结构化数组的示例:

In [539]: dt=np.dtype([('coordinates',int,(2,)),('neighbors',object)])
In [540]: arr = np.zeros((2,), dtype=dt)
In [541]: arr
Out[541]: 
array([([0, 0], 0), ([0, 0], 0)], 
      dtype=[('coordinates', '<i4', (2,)), ('neighbors', 'O')])
In [543]: arr['coordinates']=coordinates
In [544]: arr['neighbors']=neighbors
In [545]: arr
Out[545]: 
array([([100, 100], [1, 40]), ([200, 100], [0, 2, 41])], 
      dtype=[('coordinates', '<i4', (2,)), ('neighbors', 'O')])
In [546]: arr['neighbors']
Out[546]: array([[1, 40], [0, 2, 41]], dtype=object)

Notice that is basically a packaging convenience. 注意,这基本上是包装上的便利。 It stores the arrays in one place, but you still have perform your math/vector operations on the individual fields. 它将数组存储在一个位置,但是您仍然可以在各个字段上执行数学/矢量运算。

In [547]: coordinates.sum(axis=1)
Out[547]: array([200, 300])     # sum across columns of a 2d array
In [548]: neighbors.sum()
Out[548]: [1, 40, 0, 2, 41]    # sum (concatenate) of lists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM