[Numpy-discussion] record iteration (convert a 0-d array, iteration over non-sequence)

John Parejko parejkoj at speakeasy.net
Fri Jun 23 21:04:49 EDT 2006


Greetings!  I'm having trouble using records.  I'm not sure whether to 
report this as a bug, but it certainly isn't a feature!  I would like to be 
able to iterate over the individual rows in a record array, like so:

 >>> import numpy.core.records as rec
 >>> x=rec.array([[1,1.1,'1.0'],[2,2.2,'2.0']], 
formats='i4,f8,a4',names=['i','f','s'])

 >>> type(x[0])
<class 'numpy.core.records.record'>

 >>> x[0].tolist()
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
ValueError: can't convert a 0-d array to a list

 >>> [i for i in x[0]]
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: iteration over non-sequence

Am I going about this wrong?  I would think I should be able to loop over an 
individual row in a record array, or turn it into a list.  For the latter, I 
wrote my own thing, but tolist() should work by itself.  Note that in 
rec2list, I need to use range(len(line)) because the list comprehension 
doesn't work correctly:

def rec2list(line):
     """Turns a single element record array into a list."""
     return [line[i] for i in xrange(len(line))]
#...

I will file a bug, unless someone tells me I'm going about this the wrong way.

Thanks for your help
John

-- 
*************************
John Parejko
Department of Physics and Astronomy
Drexel University
Philadelphia, PA
**************************




More information about the NumPy-Discussion mailing list