[Numpy-discussion] List to Array question?

Jason.Woolard@noa... Jason.Woolard@noa...
Sat Mar 21 18:35:39 CDT 2009


hi all,

I'm sort of new to Numpy and I haven't had any luck with the docs or examples on this so I thought I would ask here. I have this small piece of code that's working but I'm wondering if the list really needs to be created or if this is an extra step that could be eliminated and speed things up a bit. It seems like the data could be dumped directly into the numpy array. 

>>> infile = file.File('test.dat',mode='r') #A binary file containing x,y,z data
>>> xdata = []                                    #Create some empty lists 
>>> ydata = []
>>> zdata = []
>>> for p in infile:
	     xdata.append(p.x)                    #Append data to list
	     ydata.append(p.y)
	     zdata.append(p.z)

>>> easting = numpy.array(xdata,dtype=float32) #Convert to array
>>> northing = numpy.array(ydata,dtype=float32)
>>> height = numpy.array(zdata,dtype=float32)
>>> print height
[-39.54999924 -39.54999924 -39.61000061 ..., -39.54000092 -39.52999878
 -39.52999878]

I also tried this and it worked but I'd have to loop through the file each time (x,y,z) and that was slower than converting from the list.

>>>xiterator = (p.x for p in infile)
>>>x = numpy.fromiter(xiterator, dtype=float32, count=-1)
>>>
>>>ziterator = (p.z for p in infile)
>>>z = numpy.fromiter(ziterator, dtype=float32, count=-1)

>From what I've read the numpy arrays need to pre-allocated and I have a header that will give me this info but I can't seem to get the data into the array.

Sorry if this is something obvious. Thanks in advance.

JW


More information about the Numpy-discussion mailing list