这很简单,在numpy
实际上。使用结构化数组 https://docs.scipy.org/doc/numpy/user/basics.rec.html:
In [21]: PosType = np.dtype([('x','f4'), ('y','f4')])
In [22]: ColorType = np.dtype([('r','f4'), ('g', 'f4'), ('b', 'f4')])
In [23]: VertexType = np.dtype([('pos', PosType),('color', ColorType)])
In [24]: VertexType
Out[24]: dtype([('pos', [('x', '<f4'), ('y', '<f4')]), ('color', [('r', '<f4'), ('g', '<f4'), ('b', '<f4')])])
In [25]: VertexType.itemsize
Out[25]: 20
然后简单地:
In [26]: vertices = np.array([( (1, 2), (3, 4, 5) ),
...: ( (6, 7), (8, 9, 10) ),
...: ( (11, 12), (13, 14, 15) )], dtype=VertexType)
In [27]: vertices.shape
Out[27]: (3,)
和基本索引:
In [28]: vertices[0]
Out[28]: (( 1., 2.), ( 3., 4., 5.))
In [29]: vertices[0]['pos']
Out[29]: ( 1., 2.)
In [30]: vertices[0]['pos']['y']
Out[30]: 2.0
In [31]: VertexType.itemsize
Out[31]: 20
numpy
曾经提供记录数组,因此您可以使用属性访问而不是索引:
In [32]: vertices = np.rec.array([( (1, 2), (3, 4, 5) ),
...: ( (6, 7), (8, 9, 10) ),
...: ( (11, 12), (13, 14, 15) )], dtype=VertexType)
In [33]: vertices[0].pos
Out[33]: (1.0, 2.0)
In [34]: vertices[0].pos.x
Out[34]: 1.0
In [35]: vertices[2].color.g
Out[35]: 14.0