如何在共享存储器中将元素列表复制到现有的numpy数组



我在共享内存中具有一个结构化的numpy数组,这只是一个更高维数组的一个"层"。

我有一个我要复制到此(sub(数组的元组列表。

我找到了如何从元组列表中制作一个新的numpy结构化数组。但是我找不到如何将此元组转换为现有的numpy(sub(数组。当然,尺寸已经匹配。

当然,我可以在python for-loop中复制Elementwise,但这似乎非常低效。我希望在numpy的基础的C 中完成循环。

说明:我的数组在共享内存中的原因是,我将其用作C 过程的常见数据,由Mutex Semaphores守护。

我的元组列表看起来像:

[(25141156064, 5.3647, 221.32287846), (25141157138, 5.3647, 73.70348602), (25141155120, 5.3646, 27.77147382), (25141160388, 5.3643, 55.5000024), (25141160943, 5.3636, 166.49511561), (25141154452, 5.3578, 92), (25141154824, 5.3539, 37.22246003), (25141155187, 5.3504, 37.22246003), (25141157611, 5.34, 915), (25141157598, 5.3329, 1047.32982582), (25140831246, 5.3053, 915), (25141165780, 5.2915, 2000), (25141165781, 5.2512, 2000), (25140818946, 5.2483, 915), (25138992274, 5.1688, 458), (25121724934, 5.1542, 458), (25121034787, 4.8993, 3.47518861), (24402133353, 2.35, 341), (24859679064, 0.8, 1931.25), (24046377720, 0.5, 100), (25141166091, 5.3783, -650.51242432), (25141165779, 5.3784, -1794.28608778), (25141157632, 5.3814, -2000), (25141157601, 5.3836, -2000), (25141164181, 5.3846, -499.65636506), (25141164476, 5.4025, -91), (25141157766, 5.4026, -634.80061236), (25141153364, 5.4034, -2000), (25141107806, 5.4035, -1601.88882309), (25141157694, 5.4136, -1047.32982582), (25141148874, 5.4278, -266), (25141078136, 5.4279, -48.4864096), (25141165317, 5.4283, -2000), (25141097109, 5.4284, -914), (25141110492, 5.4344, -774.75614589), (25141110970, 5.4502, -928.32048159), (25141166045, 5.4527, -2000), (25141166041, 5.493, -2000), (25139832350, 5.5, -10.2273)]

我的numpy数组的元素定义如下:

Id = np.uint64
Price = np.float64
Amount = np.float64
Quotation = np.dtype ([
    ('id', Id),
    ('price', Price),
    ('amount', Amount),
])
self._contents = np.ndarray (
    shape = (
        maxNrOfMarkets,
        maxNrOfItemKindsPerMarket,
        maxNrOfQuotationsPerItemKind
    )
    dtype = Quotation,
    buffer = self.sharedMemory.buf,
    offset = offset
)

如果数组未通过共享内存支持,则可以这样做。只需确保您可以正确同步访问即可。

your_array[:] = your_list

说您的 array是形状(list_length, tuples_length)

这是您要寻找的吗?

my_sub_array[:] = my_list_of_tuples

作为一个例子:

my_sub_array = np.zeros((5, 3))
my_list_of_tuples = [(i, i + 1, i + 2) for i in range(5)]
my_sub_array
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
my_sub_array[:] = my_list_of_tuples
my_sub_array
array([[0., 1., 2.],
       [1., 2., 3.],
       [2., 3., 4.],
       [3., 4., 5.],
       [4., 5., 6.]])

相关内容

  • 没有找到相关文章

最新更新