[Numpy-discussion] re carray from list of lists
maschu
m.schultz@fz-juelich...
Tue Oct 23 01:35:42 CDT 2012
Dear all,
I am new to this list and still consider myself a python newby even
though I have worked with it for almost two years now. My question may be a
bit academic (because there is numpy.genfromtext and
matplotlib.mlab.csv2rec), yet it caused me quite a bit of searching before I
found out how to convert a list of lists into a recarray. Here is an example
code:
----- start of code ----------------------------
import numpy as np
import csv
reader=csv.reader(open("csvfile.csv","rb"),delimiter=';')
x=list(reader) # array of string arrays
h=x.pop(0) # first line has column headers
# convert data to recarray (must make list of tuples!)
xx=[tuple(elem) for elem in x]
z=np.dtype(('S16, f8, f4, f4'))
res=np.array(xx,dtype=z)
res.dtype.names=h # set header names
----- end of code ----------------------------
Doesn't this consume too much memory? This first creates the list of
lists as x, then converts this into a list of tuples xx, and then forms a
recarray res from xx. Why is there the limitation that a recarray must be
given a list of tuples instead of a list of lists (which comes out of the
csv.reader, for example)?
Thanks for any hints to make this more efficient or explanations why these
things are done the way they are done.
Martin
--
View this message in context: http://old.nabble.com/recarray-from-list-of-lists-tp34590361p34590361.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.
More information about the NumPy-Discussion
mailing list