In your small example, I had trouble getting something faster than combining list comprehension and vocabulary
In [105]: timeit [{'x':i, 'y':j, 'z':k} for i,j,k in zip(x,y,z)] 100000 loops, best of 3: 15.5 Β΅s per loop In [106]: timeit [{'key':{'x':i, 'y':j, 'z':k}} for i,j,k in zip(x,y,z)] 10000 loops, best of 3: 37.3 Β΅s per loop
Alternatives that use array concatenation to concatenate arrays before splitting are slower.
In [108]: timeit [{'x':x_, 'y':y_, 'z':z_} for x_, y_, z_ in np.column_stack((x,y,z))] .... 10000 loops, best of 3: 58.2 Β΅s per loop
=========================
A structured array is easiest with recfunctions
:
In [109]: from numpy.lib import recfunctions In [112]: M=recfunctions.merge_arrays((x,y,z)) In [113]: M.dtype.names=['x','y','z'] In [114]: M Out[114]: array([(0, 10, 100), (1, 11, 101), (2, 12, 102), (3, 13, 103), (4, 14, 104), (5, 15, 105), (6, 16, 106), (7, 17, 107), (8, 18, 108), (9, 19, 109)], dtype=[('x', '<i4'), ('y', '<i4'), ('z', '<i4')]) In [115]: M['x'] Out[115]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Time is much slower, but if you want to access all x
values ββat once, it is much better than selecting from all dictionaries.
np.rec.fromarrays((x,y,z),names=['x','y','z'])
makes a repeat with the specified names. About the same speed.
I could also create an empty array of the correct type and shape and copy the arrays into it. It's possible as fast as this merge
, but harder to describe.
I would suggest optimizing the data structure for use / access, and not for building speed. Typically, you create it once and use it many times.
=============
In [125]: dt=np.dtype([('x',x.dtype),('y',y.dtype),('z',z.dtype)]) In [126]: xyz=np.zeros(x.shape,dtype=dt) In [127]: xyz['x']=x; xyz['y']=y; xyz['z']=z