[Numpy-discussion] recarray field access asymmetry
David Gowers (kampu)
00ai99@gmail....
Wed Feb 15 02:25:47 CST 2012
Hi Ben,
Thanks for your prompt response.
On Wed, Feb 15, 2012 at 6:40 PM, Benjamin Root <ben.root@ou.edu> wrote:
>
>>
>> Rather than recarrays, I just use structured arrays like so:
>>
>> A = np.array([(0, 0), (0, 0), (0, 0), (0, 0)],
>> dtype=[('x', '<u2'), ('y', '<u2')])
>>
>> I can then do:
>>
>> A['x'][0]
>>
>> Or
>>
>> A[0]['x']
>>
>> This allows me to slice and access the data any way I want. I have even
>> been able to use this dictionary idiom to format strings and such.
>>
>> Does that help?
>> Ben Root
>
> Sorry, didn't see that you have nested dtypes. Is there a particular reason
> why you need record arrays over structured arrays?
It's really a matter of how much dereferencing of substructures
occurs, and how much extra typing that turns into.
A['x'][0] -> 4 extra characters per field lookup, vs
A.x[0] / A[0].x -> 1 extra character per field lookup.
There's also an issue of highlighting -- I'd prefer x to be
highlighted in the style of an attribute, not a string, when I'm
editing source. A['x'] obviously precludes this.
I have considered normal structured arrays -- repeatedly, after being
frustrated by this recarray behaviour.
However, I'd like to achieve some kind of resolution here -- even if
it is just this unexpected behaviour being properly documented.
David
More information about the NumPy-Discussion
mailing list