Given the following arrays:
name = np.array(['a', 'b', 'c'])
val = np.array([0.4, 0.5, 0.6])
alt = np.array([1.1, 2.1, 3.1])
b = np.array([17.2])
How can I combine them into a recarray (or structured array, same thing) that looks like this: [('a', 'b', 'c'), (0.4, 0.5, 0.6), (1.1, 2.1, 3.1), (17.2)].
And where print(arr["name"]) returns ('a', 'b', 'c').
The actual data has a dozen arrays. There is always one array (b) that only has size of one; the others all have the same size, but that size will vary. So, I'm looking for a solution that is extensible to these conditions. Thank you.
The following solution produces output that closely matches what you say you desire (but it's not a NumPy record array):
This prints
['a' 'b' 'c']. Note thatarrhere is a simple dictionary.An alternative answer using NumPy's
numpy.recarraywould be the following:Here,
arrevaluates to the following: