Let’s say I have an class called Star which has an attribute color. I can get color with star.color.
But what if I have a NumPy array of these Star objects. What is the preferred way of getting an array of the colors?
I can do it with
colors = np.array([s.color for s in stars])
But is this the best way to do it?
Would be great if I could just do colors = star.color or colors = star->color etc like in some other languages. Is there
an easy way of doing this in numpy?
The closest thing to what you want is to use a
recarrayinstead of anndarrayof Python objects:prints
Using a NumPy array of Python objects usually is less efficient than using a plain
list, while arecarraystores the data in a more efficient format.