[英]How can I implement a dictionary whose value is mutable length array in numpy
我想使用numpy来实现以下数据结构。 现在,我使用python字典来完成这项工作,但是很难执行向量操作,我必须多次添加向量,因此我想使用numpy来简化工作。 主机的长度在程序执行期间会有所不同。 我是否可以使用numpy结构化数组来完成这项工作,请注意列表的长度是可变的? 我不熟悉它,只是想知道是否有可能,这样就不会浪费时间。
{
"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]
},
您显示的是一本字典,其值也是字典。 嵌套字典的某些值是标量,其他值是列表。 neighbors
列表的长度有所不同。
我可以图片创建一个结构化的数组,其中包含与内部字典键相对应的字段。
coordinates
和v-state
字段的内部尺寸甚至可以为(2,)和(4,)。
但是对于可变长度的neighbors
或hosts
,我们可以做到最好,将那些字段定义为具有对象dtype,这会将相应的列表存储在内存中的其他位置。 这种数组的数学是有限的。
但是,在您不深入了解结构化数组之前,请探索创建一组数组来存储此数据,在out字典中每个项目row
。
coordinates = np.array([[100,100],[200,100]])
neighbors = np.array([[1, 40],[0, 2, 41]])
确保您了解这些表达式产生的内容。
In [537]: coordinates
Out[537]:
array([[100, 100],
[200, 100]])
In [538]: neighbors
Out[538]: array([[1, 40], [0, 2, 41]], dtype=object)
这是可以容纳这些数组的结构化数组的示例:
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)
注意,这基本上是包装上的便利。 它将数组存储在一个位置,但是您仍然可以在各个字段上执行数学/矢量运算。
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.