We both know that: "Numpy array is multidimensional array of objects of all the same type"
However, I could create a Numpy array that contains different data types as example below. Can anyone give an explain, how it could be.
import numpy as np
a = np.array([('a',1),('b',2)],dtype=[('alpha','U11'),('num','i8')])
print(a[0][1]+1)
print(len(a[0][0]))
Output:
2
1
2 Answers 2
Those are numpy records:
Numpy provides two data structures, the homogeneous arrays and the structured (aka record) arrays. The latter one, what you just stumbled across, is a structure that not only allows you to have different data types (float, int, str, etc.) but also provides handy methods to access them, through labels for instance.
Comments
In Numpy: it's called Structured arrays
Please read more here: https://numpy.org/doc/stable/user/basics.rec.html
P/S: thanks Brandt
Comments
Explore related questions
See similar questions with these tags.
a, and itsshape. And index by fild name