Efficient way to convert numpy record array to a l

2020-06-09 04:43发布

问题:

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}]

回答1:

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}]


回答2:

This depends on the final structure required. This example shows a numpy recarray composed of several 1D subsets. To have a python dictionary instead, a possible conversion is:

import numpy as np

a = np.rec.array([np.array([1,3,6]), np.array([3.4,4.2,-1.2])], names=['t', 'x'])

b = {name:a[name] for name in a.dtype.names}


回答3:

Answered at Numpy-discussion by Robert Kern


Copy of concluding email from that discussion:

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}]

Assuming that your record array is only 1D:

In [6]: r.dtype.names Out[6]: ('name', 'age', 'weight')

In [7]: names = r.dtype.names

In [8]: [dict(zip(names, record)) for record in r] Out[8]: [{'age': 31, 'name': 'Bill', 'weight': 260.0}, {'age': 15, 'name': 'Fred', 'weight': 145.0}]



标签: python numpy