简体   繁体   English

for循环值到numpy数组中

[英]for-loop values into a numpy array

Ok, so I have wrapped a C library in Python and called the corresponding DLL's. 好的,所以我在Python中包装了一个C库,并调用了相应的DLL。 I then created a for-loop that prints out all the data points. 然后,我创建了一个for循环,可以打印出所有数据点。 Here is an small sample of my code and the library that I have wrapped: 这是我的代码和包装的库的一小部分示例:

import ctypes 
from ctypes import * 

class ParmData(Union):
_fields_ = [
         ('c', ctypes.POINTER(ctypes.c_ubyte)),
         ('f', ctypes.POINTER(ctypes.c_float))]
class SParm(Structure):
pass
SParm._fields_ = [
        ('data', ctypes.POINTER(ParmData)),
        ('time', ctypes.POINTER(ctypes.c_float))]

dll.readSParm.argtypes = (POINTER(SFile), c_char_p, c_double, c_double,    c_double, POINTER(TTag), c_ushort,)   
dll.readSParm.restype = POINTER(SParm)
g = dll.readSParm(SF, ParmName, startTime, stopTime, Null, None, convertType)
dll.freeSParm(g)

This is the for-loop that I have created in Python: 这是我在Python中创建的for循环:

for i in range(0, 50000):
print(i, (g[0].data[0].f[i]), (g[0].time[i]))

Where 哪里

(g[0].data[0].f)
(g[0].time) 

are pointers to objects that contain all of the data. 是指向包含所有数据的对象的指针。

the results of the for-loop look like this the second column is data and the third is the corresponding time value for that data: for循环的结果如下所示:第二列是数据,第三列是该数据的相应时间值:

2 -4864024.0 -1027.21630859375
3 5.114739e-43 -1026.21630859375
4 2.840103e-37 -1025.21630859375
5 2.250064e-38 -1024.21630859375

My question is this: 我的问题是这样的:

How can I get this data into a numpy array? 如何将这些数据放入numpy数组? Because I have so many data point I cant type them all in. 因为我有很多数据点,所以无法全部输入。

A dirty (yet easy) way would be to just iterate over the data and fill the array as you go, like so: 一种肮脏(但很简单)的方法是仅遍历数据并随行填充数组,如下所示:

data_len = 5000 #<- you should have a way of knowing how much data there is
arr = np.empty([data_len,2],dtype=np.float) #empty array of the right size 
for i in range(data_len): #we fill the array row by row
    arr[i,:]= (g[0].data[0].f[i],g[0].time[i])

print(arr)

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

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