[Numpy-discussion] adding field to rec array

Robert Kern robert.kern at gmail.com
Fri Oct 5 14:05:56 EDT 2007


John Hunter wrote:
> On 9/26/07, Robert Kern <robert.kern at gmail.com> wrote:
> 
>> Here is the straightforward way:
>>
>> In [15]: import numpy as np
>>
>> In [16]: dt = np.dtype([('foo', int), ('bar', float)])
>>
>> In [17]: r = np.zeros((3,3), dtype=dt)
> 
> Here is a (hopefully) simple question.  If I create an array like
> this, how can I efficiently convert it to a record array which lets me
> do r.attr in addition to r['attr'].  I'm pretty addicted to the former
> syntax.

In [32]: r2 = r.view(np.recarray)

In [33]: r2.foo
Out[33]:
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 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



More information about the NumPy-Discussion mailing list