简体   繁体   English

如何在Python中将数据从内存复制到numpy数组

[英]How to copy data from memory to numpy array in Python

For example, I have a variable which point to a vector contains many elements in memory, I want to copy element in vector to a numpy array, what should I do except one by one copy? 例如,我有一个指向向量的变量,该向量包含内存中的许多元素,我想将向量中的元素复制到一个numpy数组中,除了一张一张地复制,我该怎么办? Thx 谢谢

I am assuming that your vector can be represented like that:- 我假设您的向量可以这样表示:

import array

x = array('l', [1, 3, 10, 5, 6])   # an array using python's built-in array module

Casting it as a numpy array will then be:- 然后将其numpy转换为numpy数组:

import numpy as np

y = np.array(x)

如果数据以本机浮点格式打包在缓冲区中:

a = numpy.fromstring(buf, dtype=float, count=N)

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

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