Efficiently converting numpy record array to a list of dictionary

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

On Thu, Feb 4, 2010 at 2:26 PM, Vishal Rana <ranavishal@gmail.com> wrote:
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}]
It looks like a two-body problem, so it should be solvable.

On Thu, Feb 4, 2010 at 3:46 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
On Thu, Feb 4, 2010 at 2:26 PM, Vishal Rana <ranavishal@gmail.com> wrote:
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}]
It looks like a two-body problem, so it should be solvable.
Do you already have recs as a list? Then:
recs = [('Bill', 31, 260.0), ('Fred', 15, 145.0)] [{'name': rec[0], 'age': rec[1], 'weight': rec[2]} for rec in recs]
[{'age': 31, 'name': 'Bill', 'weight': 260.0}, {'age': 15, 'name': 'Fred', 'weight': 145.0}]

On Thu, Feb 4, 2010 at 16:26, Vishal Rana <ranavishal@gmail.com> wrote:
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}] -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

Vishal, Robert's code does the trick, but--in case you are new to numpy record arrays-I thought I'd point out that the array itself already acts like a list of dictionaries: In [6]: import numpy as np In [7]: dt = np.dtype([('name', 'S30'),('age',int),('weight',float)]) In [8]: r = np.array([('Bill',31, 260.0), ('Fred', 15, 145.0)], dtype=dt) In [9]: r[0]['name'] Out[9]: 'Bill' In [10]: r[1]['age'] Out[10]: 15 Warren Robert Kern wrote:
On Thu, Feb 4, 2010 at 16:26, Vishal Rana <ranavishal@gmail.com> wrote:
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}]

Warren, thanks for the information. <http://www.brainyquote.com/quotes/authors/c/charles_de_gaulle.html> Vishal Charles de Gaulle<http://www.brainyquote.com/quotes/authors/c/charles_de_gaulle.html> - "The better I get to know men, the more I find myself loving dogs." On Thu, Feb 4, 2010 at 4:10 PM, Warren Weckesser < warren.weckesser@enthought.com> wrote:
Vishal,
Robert's code does the trick, but--in case you are new to numpy record arrays-I thought I'd point out that the array itself already acts like a list of dictionaries:
In [6]: import numpy as np
In [7]: dt = np.dtype([('name', 'S30'),('age',int),('weight',float)])
In [8]: r = np.array([('Bill',31, 260.0), ('Fred', 15, 145.0)], dtype=dt)
In [9]: r[0]['name'] Out[9]: 'Bill'
In [10]: r[1]['age'] Out[10]: 15
Warren
Robert Kern wrote:
On Thu, Feb 4, 2010 at 16:26, Vishal Rana <ranavishal@gmail.com> wrote:
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}]
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

Thanks Robert :) Vishal Rana Samuel Goldwyn<http://www.brainyquote.com/quotes/authors/s/samuel_goldwyn.html> - "I don't think anyone should write their autobiography until after they're dead." On Thu, Feb 4, 2010 at 4:04 PM, Robert Kern <robert.kern@gmail.com> wrote:
On Thu, Feb 4, 2010 at 16:26, Vishal Rana <ranavishal@gmail.com> wrote:
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}]
-- Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
Keith Goodman
-
Robert Kern
-
Vishal Rana
-
Warren Weckesser