I would like to combine several lists or arrays into a single record array.
In the following code I want to created a record array with two colums: "a" and "b". The first column will contain letters from "a" to "j", the second one will contain numbers from 0 to 9
In [22]: a = list('abcdefghij'); b = range(10); c = numpy.vstack((a, b)).T
In [23]: desc = {'names': ('a', 'b'), 'formats': ('S4', 'f4')}
In [24]: d = numpy.array(c, dtype=desc)
In [25]: d
Out[25]:
array([[('a', 0.0), ('0', 0.0)],
[('b', 0.0), ('1', 0.0)],
[('c', 0.0), ('2', 0.0)],
[('d', 0.0), ('3', 0.0)],
[('e', 0.0), ('4', 0.0)],
[('f', 0.0), ('5', 0.0)],
[('g', 0.0), ('6', 0.0)],
[('h', 0.0), ('7', 0.0)],
[('i', 0.0), ('8', 0.0)],
[('j', 0.0), ('9', 0.0)]],
dtype=[('a', '|S4'), ('b', '<f4')])
In [26]: d['a']
Out[26]:
array([['a', '0'],
['b', '1'],
['c', '2'],
['d', '3'],
['e', '4'],
['f', '5'],
['g', '6'],
['h', '7'],
['i', '8'],
['j', '9']],
dtype='|S4')
In [27]: d['b']
Out[27]:
array([[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.],
[ 0., 0.]], dtype=float32)
The result is completely not what I would expect. What I want is:
In [XX]: d['a']
Out[XX]: array(['a', 'b', 'c', ..., 'j'])
In [XX]: d['b']
Out[XX]: array([1., 2., 3., ..., 9.])
EDIT
My goal was to be able to create record arrays from already existing numpy.array, and not only from the individual lists. Following the answer by Sven Marnach, I had several tries and errors and this is what I got:
d = array(map(lambda l: tuple(l[0]), zip(c)), dtype=desc)
This seems to be a pretty ugly solution. Does anyone has a better one?
Following from your comment on Sven’s answer, if you don’t have
aandb, then do the following and populateclater,gives,