I can solve the problem by making a list of the original array:
In [9]: numbers = np.array([constants],dtype=[('constants',dt)])
In [10]: numbers
Out[10]:
array([[(('pi', 3.14),), (('e', 2.72),)]],
dtype=[('constants', [('name', '|S2'), ('val', '<f8')])])
But when I look at it, of course I have an extra nesting in the list:
In [11]: numbers['constants']
Out[11]:
array([[('pi', 3.14), ('e', 2.72)]],
dtype=[('name', '|S2'), ('val', '<f8')])
In [12]: numbers['constants']['name']
Out[12]:
array([['pi', 'e']],
dtype='|S2')
I really just want the first item in the list:
In [13]: numbers[0]
Out[13]:
array([(('pi', 3.14),), (('e', 2.72),)],
dtype=[('constants', [('name', '|S2'), ('val', '<f8')])])
I can also achieve this by flattening the array afterward:
In [14]: numbers.flatten()
Out[14]:
array([(('pi', 3.14),), (('e', 2.72),)],
dtype=[('constants', [('name', '|S2'), ('val', '<f8')])])
In [15]: numbers.flatten()['constants']
Out[15]:
array([('pi', 3.14), ('e', 2.72)],
dtype=[('name', '|S2'), ('val', '<f8')])
In [16]: numbers.flatten()['constants']['name']
Out[16]:
array(['pi', 'e'],
dtype='|S2')
But isn't that a hack? What I really want in the end is this array:
In [17]: numbers = np.array([(('pi', 3.14),), (('e', 2.72),)],dtype=[('constants',dt)])
In [18]: numbers['constants']
Out[18]:
array([('pi', 3.14), ('e', 2.72)],
dtype=[('name', '|S2'), ('val', '<f8')])
In [19]: numbers['constants']['name']
Out[19]:
array(['pi', 'e'],
dtype='|S2')
Without having to make a one-item list and then flatten it. Any better ideas?