tags:

views:

151

answers:

2

How do I convert the numpy record array below:

recs = [('Bill', 31, 260.0), ('Fred', 15, 145.0)]
r = rec.fromrecords(recs, names='name, age, weight', formats='S30, i2, f4')

to a list of dictionary like:

[{'name': 'Bill', 'age': 31, 'weight': 260.0}, 
'name': 'Fred', 'age': 15, 'weight': 145.0}]
+3  A: 

I am not sure there is built-in function for that or not, but following could do the work.

>>> [dict(zip(r.dtype.names,x)) for x  in r]
[{'age': 31, 'name': 'Bill', 'weight': 260.0}, 
{'age': 15, 'name': 'Fred', 'weight': 145.0}]
S.Mark
Probably worth pulling `r.dtype.names` out to a local variable. Possibly `izip` is faster than `zip` as the number of records increases
gnibbler
A: 

Answered at Numpy-discussion by Robert Kern

Vishal